使用Controller.UpdateModel方法来更新ViewModel
2011-09-11 13:10
519 查看
原文(En):http://www.joe-stevens.com/2010/02/17/asp-net-mvc-using-controller-updatemodel-when-using-a-viewmodel/
在MVC中,更新Model的时候,我们通常使用Controller.UpdateModel方法。我最近在项目中使用了自定义ViewModel,在使用UpdateModel方法的时候无法映射到Post的数据到Model对象里。这个解决方案是一个简单的,但并不像它应该的IntelliSense不拾取方法的重载泛型类型推断时。
当不使用ViewModel的时候,我可以像这样使用Controller方法:
前端View中使用强类型视图:
Controller中可以用这样的方法来保存Student数据:
UpdateModel会遍历formValues集合并且映射post过来的数据到student model对象中。如果我去检查这个集合的Keys,我可以看到formValues匹配了student model的属性,使得映射成功。
现在来看看如果使用ViewModel会发生什么,这是一个很好的MVC练习。
下面是一个Student ViewModel的代码示例:
接下来是如何使用ViewModel来编辑的方法:
前端View视图页代码:
我们设断点来看下这次Post到Edit方法的formValues集合,Keys如图所示:
可以看到Keys中包含了Student这个Model对象,这是因为ViewModel使用了这个Student Model,但增加了“Student”作为前缀,和前面直接使用Model不一样。如果按照前面的UpdateModel方法,程序会获取不到post过来的值,也会报错。在这里,UpdateModel有重载方法,第二参数是Prefix,也就是"Student"前缀。
我们这下写这个UpdateModel方法:
你可以看到使用了“Student”作为前缀,映射正确。
程序运行成功!
特别注意:在增加了Prefix后,如果Post过来Form中有不等于Prefix的Name,UpdateModel的时候会报错。一旦增加Prefix前缀后,Post过来的FormCollection必须都含有该前缀对象才可以Work。
希望该文章会给大家带来帮助!
在MVC中,更新Model的时候,我们通常使用Controller.UpdateModel方法。我最近在项目中使用了自定义ViewModel,在使用UpdateModel方法的时候无法映射到Post的数据到Model对象里。这个解决方案是一个简单的,但并不像它应该的IntelliSense不拾取方法的重载泛型类型推断时。
当不使用ViewModel的时候,我可以像这样使用Controller方法:
public ActionResult Edit(int id) { Student student = context.Student.FirstOrDefault(s => s.ID.Equals(id)); return View(student); }
前端View中使用强类型视图:
<div> <%= Html.LabelFor(model => model.Forename) %> </div> <div> <%= Html.TextBoxFor(model => model.Forename)%> <%= Html.ValidationMessageFor(model => model.Forename)%> </div> <div> <%= Html.LabelFor(model => model.Surname)%> </div> <div> <%= Html.TextBoxFor(model => model.Surname)%> <%= Html.ValidationMessageFor(model => model.Surname)%> </div>
Controller中可以用这样的方法来保存Student数据:
[HttpPost] public ActionResult Edit(int id, FormCollection formValues) { try { Student student = context.Student.FirstOrDefault(s => s.ID.Equals(id)); UpdateModel(student); context.SaveChanges(); return RedirectToAction("Details", new { id = student.ID }); } catch { throw; } }
UpdateModel会遍历formValues集合并且映射post过来的数据到student model对象中。如果我去检查这个集合的Keys,我可以看到formValues匹配了student model的属性,使得映射成功。
现在来看看如果使用ViewModel会发生什么,这是一个很好的MVC练习。
下面是一个Student ViewModel的代码示例:
public class StudentModel { public Student Student { get; set; } public SelectList Titles { get; set; } public StudentModel() { } public StudentModel(Student student, IEnumerable<Title> titles) { Student = student; Titles = new SelectList(titles, "ID", "Name", student.TitleID); } }
接下来是如何使用ViewModel来编辑的方法:
public ActionResult Edit(int id) { Student student = context.Student.FirstOrDefault(s => s.ID.Equals(id)); return View(new StudentModel(student, context.Title)); }
前端View视图页代码:
<div> <%= Html.LabelFor(model => model.Student.Title) %> </div> <div> <%= Html.DropDownListFor(model => model.Student.TitleID, Model.Titles)%> </div> <div> <%= Html.LabelFor(model => model.Student.Forename) %> </div> <div> <%= Html.TextBoxFor(model => model.Student.Forename)%> <%= Html.ValidationMessageFor(model => model.Student.Forename)%> </div>
我们设断点来看下这次Post到Edit方法的formValues集合,Keys如图所示:
可以看到Keys中包含了Student这个Model对象,这是因为ViewModel使用了这个Student Model,但增加了“Student”作为前缀,和前面直接使用Model不一样。如果按照前面的UpdateModel方法,程序会获取不到post过来的值,也会报错。在这里,UpdateModel有重载方法,第二参数是Prefix,也就是"Student"前缀。
我们这下写这个UpdateModel方法:
UpdateModel<Student>(student, "Student");
你可以看到使用了“Student”作为前缀,映射正确。
[HttpPost] public ActionResult Edit(int id, FormCollection formValues) { try { Student student = context.Student.FirstOrDefault(s => s.ID.Equals(id)); UpdateModel(student, "Student"); context.SaveChanges(); return RedirectToAction("Details", new { id = student.ID }); } catch { throw; } }
程序运行成功!
特别注意:在增加了Prefix后,如果Post过来Form中有不等于Prefix的Name,UpdateModel的时候会报错。一旦增加Prefix前缀后,Post过来的FormCollection必须都含有该前缀对象才可以Work。
希望该文章会给大家带来帮助!
相关文章推荐
- 使用Controller.UpdateModel方法来更新ViewModel
- 使用MFMailComposeViewController在iOS应用内编辑邮件使用方法及常见问题
- pushViewController/presentModalViewController/addSubView区别及使用方法
- UIPageViewController最简单的使用方法
- ajax中 同一页面中 UpdatePanel 区别 更新多个区域的关键 timer使用方法
- pushViewController/presentModalViewController/addSubView区别及使用方法
- 不使用nib 文件时,需要改变一个view 的大小时,需要为viewcontroller添加loadView方法
- UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)
- IOS 使用presentViewController方法跳转的navigationController如何获取及视图层级关系
- PresentModalViewController 的正确使用方法
- 使用dismissModalViewControllerAnimated 不调用dealloc 内存不释放解决方法
- C#使用SqlDataAdapter的Update方法更新数据
- iOS开发之transitionFromViewController方法的使用
- UIAlertViewController的使用方法
- iOS开发中ViewController的生命周期分析和使用方法
- transitionFromViewController方法的使用
- Silverlight + WCF使用Linq to SQL以及ADO.NET Entity Data Model更新数据库子表方法
- transitionFromViewController方法的使用
- 解决asp.net mvc UpdateModel更新对象后出现null问题的方法
- iOS UICollectionViewController基本使用方法和简单的相册实现