MVC中,提交后界面Model值未能更新的原因及优化。
2012-08-10 16:05
417 查看
假设有如下代码:
View/Time/index.cshtml
TimeController.cs
结果如下:
点击submit之后
点击后发现,textboxfor中的值并没有改变,但是model的值其实已经改变了。
这是因为controller的ModelState属性,是dictionary类型,它包含了所有的要提交的值和验证的错误信息。在返回View的时候,文本框之类的值由ModelState所持有,因此没有得到更新,无法在界面上显示。最简单的做法就是使用ModelState.Clear(),或者ModelState.Remove("currenttime"); 之类的方法。但是也存在一些风险,比如把验证的错误信息也清除了。
先看一下效果。
但是这种方法并不安全,因为如果是clear方法可能会清空ModelState中验证信息。而且刷新页面的时候,也会有重复提交的风险。
改成如下方式,把post方式转换成get方式,这样就避免了刷新时候会弹出重复提交的按钮。用TempData或者Session作为临时保存数据。
本文出自 “一只博客” 博客,请务必保留此出处http://cnn237111.blog.51cto.com/2359144/960369
View/Time/index.cshtml
@model MvcApplication1.Models.timeModel <h2>Time</h2> @using (Html.BeginForm("Index", "Time")) { <B>Get Current time:</B> @Html.TextBoxFor(m => m.currenttime)<br /> <B>Current time:</B> @Model.currenttime<br /> <p> <input type="submit" value="search" /> </p> }
TimeController.cs
public ActionResult Index() { MvcApplication1.Models.timeModel model = new Models.timeModel(); model.currenttime = DateTime.Now.TimeOfDay; return View(model); } [HttpPost] public ActionResult Index(MvcApplication1.Models.timeModel model) { model.currenttime = DateTime.Now.TimeOfDay; return View(model); }
结果如下:
点击submit之后
点击后发现,textboxfor中的值并没有改变,但是model的值其实已经改变了。
这是因为controller的ModelState属性,是dictionary类型,它包含了所有的要提交的值和验证的错误信息。在返回View的时候,文本框之类的值由ModelState所持有,因此没有得到更新,无法在界面上显示。最简单的做法就是使用ModelState.Clear(),或者ModelState.Remove("currenttime"); 之类的方法。但是也存在一些风险,比如把验证的错误信息也清除了。
先看一下效果。
public ActionResult Index() { MvcApplication1.Models.timeModel model = new Models.timeModel(); model.currenttime = DateTime.Now.TimeOfDay; return View(model); } //每次post重新赋值时间给model [HttpPost] public ActionResult Index(MvcApplication1.Models.timeModel model) { ModelState.Remove("currenttime"); model.currenttime = DateTime.Now.TimeOfDay; return View(model); }
但是这种方法并不安全,因为如果是clear方法可能会清空ModelState中验证信息。而且刷新页面的时候,也会有重复提交的风险。
改成如下方式,把post方式转换成get方式,这样就避免了刷新时候会弹出重复提交的按钮。用TempData或者Session作为临时保存数据。
public ActionResult Index() { Models.timeModel model = new Models.timeModel(); if (TempData["timeModel"] != null) { model = TempData["timeModel"] as Models.timeModel; } else { model.currenttime = DateTime.Now.TimeOfDay; } return View(model); } //把post方式转换成get方式 [HttpPost] public ActionResult Index(MvcApplication1.Models.timeModel model) { model.currenttime = DateTime.Now.TimeOfDay; TempData["timeModel"] = model; return RedirectToAction("Index"); }
本文出自 “一只博客” 博客,请务必保留此出处http://cnn237111.blog.51cto.com/2359144/960369
相关文章推荐
- 关于Asp.Net MVC 中 UpdateModel 的未能更新***模型的 解决方案!
- MVC UpdateModel的未能更新XXXXX的类型模型
- 关于Asp.Net MVC 中 UpdateModel 的未能更新***模型的 解决方案!
- MVC UpdateModel的未能更新XXXXX的类型模型
- 关于MVC UpdateModel的未能更新XXXXX的类型模型
- MVC学习6 学习使用Code First Migrations功能 把Model的更新同步到DB中
- vs2013 system.web.mvc未能加载原因(已解决)
- App提交审核被拒的原因汇总(不断更新...)
- 在MVC中更新Model-First Entity Framework POCO实体外键的方法
- 解决asp.net mvc UpdateModel更新对象后出现null问题的方法
- ASP.NET MVC TryUpdateModel 更新model
- asp.net mvc 提交model 接收不了
- 解决asp.net mvc UpdateModel更新对象后出现null问题的方法
- model中的layoutChanged ()信号 通知视图更新界面
- 如何在ASP.NET MVC中实现提交若干个某模型的数据(某Model的List或ICollection,大小不定)
- ASP.NET MVC前端提交List<Model>到controller
- Asp.net MVC中提交集合对象,实现Model绑定
- Asp.net MVC中提交集合对象,实现Model绑定
- 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (一)
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(十四)谈谈写博客的原因和项目优化