What is correct behaviour of UpdateModel in ASP.NET MVC?

前端 未结 4 580
轮回少年
轮回少年 2020-12-24 08:02

I am interested to know what you guys feel should be deemed \"correct behaviour\" in terms of the UpdateModel method in ASP.NET MVC.

The reason I ask he

4条回答
  •  -上瘾入骨i
    2020-12-24 08:57

    The behavior you're experiencing with UpdateModel() sounds like you're list binding, in which case UpdateModel() will blow away the contents of the list and repopulate it. See Hanselman's blog for a discussion on this. If you're updating a single object, UpdateModel() will update that individual object, leaving properties that don't have a corresponding form value as-is.

    Many of these problems boil down to that UpdateModel() is really meant to repopulate view models - not domain models - based on form input. (I'm slightly simplifying things by saying that a view model is just a contract between a controller and the view, while your domain model might be a LINQ2SQL or EF model object.) All of the MVC tutorials and demos show UpdateModel() being used against database objects, which I feel is unfortunate since it's somewhat misleading as to the intended purpose of model binding. Robert's post is more indicative of the actual intent of UpdateModel().

提交回复
热议问题