How to update Model when binding to a ViewModel?

前端 未结 3 1631
予麋鹿
予麋鹿 2021-02-01 22:42

I have an [HttpPost] action method signature like this:

[HttpPost]
public ActionResult Edit(ExistingPostViewModel model)
{
   // Save the edited Pos         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 23:17

    For simple things where you don't have to run any controls prior to implementing the update what you are doing is okay (db.get(), and then update).

    When things get complicated, you have to load the entity, and then select and apply user's changes from the view model, property by property. In those cases, you end up writing Update methods, which gets the new data as input, and then you load the existing entity, then compare states, and take the required actions based on the view model data. Actually in this case, probably you wont have an Update method but will have behaviors, like CancelPost, AddComment, EditPost (which also logs the edit reason), AddTagsToPost etc.

提交回复
热议问题