asp.net-mvc-3 – 使用redirectAction和prg模式在操作之间发送数据
发布时间:2020-05-23 22:54:17 所属栏目:asp.Net 来源:互联网
导读:我如何使用redirectAction在动作之间发送数据? 我正在使用PRG模式.我想做出类似的东西 [HttpGet] [ActionName(Success)] public ActionResult Success(PersonalDataViewModel model) { //model ko if (model == null
|
我如何使用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 });
}
解决方法重定向时,您只能传递查询字符串值.不是完整的复杂对象:return RedirectToAction("Success",new {
prop1 = model.Prop1,prop2 = model.Prop2,...
});
这仅适用于标量值.因此,您需要确保在查询字符串中包含所需的每个属性,否则它将在重定向中丢失. 另一种可能性是将模型保存在服务器上的某个位置(如数据库或其他东西),并且在重定向时只传递允许检索模型的id: int id = StoreModel(model);
return RedirectToAction("Success",new { id = id });
并在Success操作内部检索模型: public ActionResult Success(int id)
{
var model = GetModel(id);
...
}
另一种可能性是使用TempData,虽然我个人不推荐它: TempData["model"] = model;
return RedirectToAction("Success");
并在Success操作中从TempData获取它: var model = TempData["model"] as PersonalDataViewModel; (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何调试Minification失败 返回未最终内容
- asp.net-mvc-3 – MVC 3 – 比较属性 – 在客户端执行不区分
- asp.net-core – 在ASP.net Core中使用BeginCollectionItem
- asp.net-mvc – DisplayFor和ValueFor之间的区别
- asp.net-mvc – MVC 4 HttpNotFound()和404错误
- asp.net-mvc – Razor View语法无法识别HTML属性中的“@”
- asp.net – 如何配置IIS以便在连接到SQL Server时使用用户的
- asp.net – 基于用户更改主题/ CSS
- asp.net – 具有一个无效的SelectedValue,因为它在项目列表
- asp.net – 返回按钮刷新页面
