问题描述
我的工作我的第一个.NET MVC应用程序和使用的NerdDinner教程作为参考点。这是耐人寻味我此刻的一点是的UpdateModel()
方法。 (我不喜欢使用的事情,我真的不明白。)
I'm working on my first .NET MVC application and using the NerdDinner tutorial as a reference point. One point that is intriguing me at the moment is the UpdateModel()
method. (I don't like using things I don't really understand.)
从拍摄 -
//
// POST: /Dinners/Edit/2
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues) {
Dinner dinner = dinnerRepository.GetDinner(id);
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
我的主要问题是如何做的的UpdateModel()
可以访问的编辑方法通过formValues?为什么收集没有明确作为参数传递给方法?
My main question is how does the UpdateModel()
get access to the formValues passed in the Edit method? Why is the collection not passed in explicitly as a parameter to the method?
推荐答案
的UpdateModel()是尝试绑定了一堆不同的输入数据源(HTTP POST数据视图的到来,查询字符串值,会话变量控制器的辅助方法/饼干等),以表明你作为一个参数的显式模型对象。本质上,它仅用于模型绑定。
UpdateModel() is a Controller helper method that attempts to bind a bunch of different input data sources (HTTP POST data coming from a View, QueryString values, Session variables/Cookies, etc.) to the explicit model object you indicate as a parameter. Essentially, it is only for model binding.
如果您的前preSS为你这种行为是强类型模型(如视图模型)的输入参数,你已经采取了所有已在幕后进行的步骤时的UpdateModel()被调用。如果检索从在DataContext对象和编辑其属性,调用SaveChanges()是所有你需要推更新回数据库(在这种情况下,保存())。
If you express the input parameters for your Action as a strongly-typed model (like a View Model), you've already taken all of the steps that are done behind the scenes when UpdateModel() is called. If you retrieve an object from the DataContext and edit its properties, SaveChanges() is all you need to push the updates back to the database (in this case, Save()).
例如:
//
// POST: /Dinners/Edit/2
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(DinnerViewModel incoming) {
var dinner = dinnerRepository.GetDinner(incoming.DinnerID);
dinner.Description = incoming.Description;
dinnerRepository.Save();
return RedirectToAction("Details", new { id = incoming.DinnerID });
}
然而,有一个用例使用的UpdateModel()有一个强类型的模型:当你传递一个强类型的模型,并希望自己的价值观,直接替换那些的(条件是它们所有名称和类型相同)从数据库实体。在这种情况下,你将检索对象,它使用的UpdateModel(),它的模型绑定操作将在任何类似命名和类型属性拉离强类型对象来检索对象。换句话说,它将执行反射为您服务。
However, there is a use-case for using UpdateModel() with a strongly-typed model: when you are passing in a strongly-typed model and want its values to directly replace those of an entity from the database (provided they are all named and typed the same). In this case, you would retrieve the object, use UpdateModel() on it, and its model binding operation will pull in any similarly-named and typed properties from the strongly-typed object to the retrieved object. In other words, it will perform reflection for you.
所以,就像你的例子,如果你想所有属性,而不指定哪些更新,你的强类型模型和数据库模型有类似命名的属性更新,你仍然要使用的UpdateModel()趁反射。
So, like your example, if you want all properties to update without specifying which to update, and your strongly-typed model and database model have similarly-named properties, you would still want to use UpdateModel() to take advantage of the reflection.
例如:
//
// POST: /Dinners/Edit/2
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(DinnerViewModel incoming) {
var dinner = dinnerRepository.GetDinner(incoming.DinnerID);
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = incoming.DinnerID });
}
在这里(在使用的FormCollection对象),唯一的好处是,你可以访问强类型对象的所有其他属性(如图incoming.DinnerID)。
The only advantage here (over using a FormCollection object) is that you'd have access to all other properties of the strongly-typed object (as shown by incoming.DinnerID).
结论:如果你翻译一个强类型的对象派生类对象,它可能是最容易使用的UpdateModel()。然而,这大可不必,如果你只是更新派生对象的一些属性。此外,要注意,使用实体框架(而不是像LINQ到SQL)使得这一切毫无意义的,因为它可以与强类型的对象和派生的对象有自己的方法。
Conclusion: if you're translating a strongly-typed object to a derived object, it's probably easiest to use UpdateModel(). However, it's largely unnecessary if you are simply updating a few properties of the derived object. Also, be aware that use of the Entity Framework (instead of something like Linq to SQL) makes all of this moot, as it can relate strongly-typed objects and derived objects with its own methods.
这篇关于如何在ASP.NET MVC的UpdateModel()方法的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!