将数据从Action传递给另一个Action

scv*_*scv 5 asp.net-mvc-3

您如何通过RedirectAction方法将模型从(GetDate)操作传递到另一个(ProcessP)操作?

这是源代码:

[HttpPost]
public ActionResult GetDate(FormCollection values, DateParameter newDateParameter)
{
    if (ModelState.IsValid)
    {
return RedirectToAction("ProcessP");
    }
    else
    {
return View(newDateParameter);
    }
}


public ActionResult ProcessP()
{
   //Access the model from GetDate here??
    var model = (from p in _db.blah
 orderby p.CreateDate descending
 select p).Take(10);

    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

Jes*_*sse 7

如果您需要将数据从一个操作传递到另一个操作,则一个选项是使用TempData.例如,在GetDate中,您可以将数据添加到会话中,如下所示:

TempData["Key"] = YourData
Run Code Online (Sandbox Code Playgroud)

然后执行重定向.在ProcessP中,您可以使用以前使用的密钥访问数据:

var whatever = TempData["Key"];
Run Code Online (Sandbox Code Playgroud)

对于一个体面的阅读,我建议通过这个线程阅读:ASP.NET MVC - TempData - 好的或坏的做法