我如何使用redirectAction在动作之间发送数据?
我正在使用PRG模式.我想做出类似的东西
[HttpGet]
[ActionName("Success")]
public ActionResult Success(PersonalDataViewModel model)
{
//model ko
if (model == null)
return RedirectToAction("Index", "Account");
//model OK
return View(model);
}
[HttpPost]
[ExportModelStateToTempData]
[ActionName("Success")]
public ActionResult SuccessProcess(PersonalDataViewModel model)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("", "Error");
return RedirectToAction("Index", "Account");
}
//model OK
return RedirectToAction("Success", new PersonalDataViewModel() { BadgeData = this.GetBadgeData });
}
Run Code Online (Sandbox Code Playgroud)
重定向时,您只能传递查询字符串值.不是完整的复杂对象:
return RedirectToAction("Success", new {
prop1 = model.Prop1,
prop2 = model.Prop2,
...
});
Run Code Online (Sandbox Code Playgroud)
这仅适用于标量值.因此,您需要确保在查询字符串中包含所需的每个属性,否则它将在重定向中丢失.
另一种可能性是将模型保存在服务器上的某个位置(如数据库或其他东西),并且在重定向时只传递允许检索模型的id:
int id = StoreModel(model);
return RedirectToAction("Success", new { id = id });
Run Code Online (Sandbox Code Playgroud)
并在Success操作内部检索模型:
public ActionResult Success(int id)
{
var model = GetModel(id);
...
}
Run Code Online (Sandbox Code Playgroud)
另一种可能性是使用TempData虽然我个人不推荐它:
TempData["model"] = model;
return RedirectToAction("Success");
Run Code Online (Sandbox Code Playgroud)
并在Success动作内部从TempData以下位置获取:
var model = TempData["model"] as PersonalDataViewModel;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9889 次 |
| 最近记录: |