asp.net-mvc-3 – 使用redirectAction和prg模式在操作之间发送数
发布时间:2020-12-16 00:00:05 所属栏目:asp.Net 来源:网络整理
导读:我如何使用redirectAction在动作之间发送数据? 我正在使用PRG模式.我想做出类似的东西 [HttpGet] [ActionName("Success")] public ActionResult Success(PersonalDataViewModel model) { //model ko if (model == null) return RedirectToAction("Index","A
我如何使用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-2 – 如何在asp.net mvc2中的控制器外部获取请
- asp.net – App_Data / ASPNETDB.MDF到Sql Server 2005(或0
- asp.net – 什么时候应该使用工厂模式?
- 单元测试 – 有没有办法单元测试在视图中设置的ASP.NET MVC
- asp.net必填字段验证器禁用输入按钮表单提交
- asp.net-mvc – Windows Azure多租户应用程序和SSL
- asp.net-mvc – ASP.NET MVC – HttpException或返回视图?
- 十分钟搭建自己的私有NuGet服务器-BaGet
- 在ASP.NET 5项目中运行NUnit测试(在VS Test Explorer中)
- asp.net – 第一次访问网站需要非常长的时间(总共最多68秒)
推荐文章
站长推荐
- asp.net – 单击时更改asp:CheckBox的文本
- ASP.NET JSON字符串与实体类的互转换示例代码
- asp.net-mvc – 我应该如何管理ASP.NET MVC 4应用
- .net – 当表中的数据发生变化时,SqlCacheDepend
- asp.net-mvc – 在自定义Html帮助器中访问模型对
- asp.net-mvc-4 – MVC4如何挂钩OnSessionStart事
- asp.net-mvc – 如何在ASP.NET MVC的一个视图中使
- ASP.NET 2.0 JQuery AJAX登录
- 水平asp.net菜单显示为垂直列表
- asp.net – MVC 4 – 在局部视图中使用不同的模型
热点阅读