Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update Model when binding to a ViewModel?

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

[HttpPost]
public ActionResult Edit(ExistingPostViewModel model)
{
   // Save the edited Post.
}

Now, in the past (when i didn't use ViewModels, e.g R&D), i had an implementation of an Edit method like this:

[HttpPost]
public ActionResult Edit(Post model)
{
    var existingPost = repo.Find(model.Id);
    TryUpdateModel(existingPost);
    repo.Save(existingPost);  
    return RedirectToAction("Success", existingPost.Id);
}

Which worked great.

But i'm confused how to adapt the above to the ViewModel approach.

If i do this:

TryUpdateModel(existingPost)

With my ViewModel approach, not much happens. No errors, but nothing is being updated because MVC won't know how to update a Post from a ExistingPostViewModel (before it was Post -> Post).

Now, i'm using AutoMapper. So i thought i could map from the ViewModel to the Post, then save the post.

But then im basically overriding everything. Which i don't want to do and defeats the point of the cut down ViewModel.

Can anyone un-confuse me?

This seems like a really common scenario, and i am totally stumped as to how people solve this. I can only see 3 possible solutions:

  1. Don't use a ViewModel in the HTTP POST. As i said i did this in the past for R&D and it works, but now i see how my View's have evolved (validation, simplicity), and i can't compromise that just for the sake of this problem.

  2. Don't use TryUpdateModel. Possibly, but then how would i merge in the changes?

  3. Use left-to-right. Ugh. But at the moment this seems to be the way im leaning.

Someone please give me solution #4! :)

BTW, i'm using ASP.NET MVC 3, Razor and Entity Framework.

like image 743
RPM1984 Avatar asked May 05 '11 05:05

RPM1984


People also ask

How the model changes could be notified to ViewModel in MVVM?

If you want your Models to alert the ViewModels of changes, they should implement INotifyPropertyChanged, and the ViewModels should subscribe to receive PropertyChange notifications. But typically this is only needed if more than one object will be making changes to the Model's data, which is not usually the case.

Should ViewModel implement INotifyPropertyChanged?

You should always have the Model implement INotifyPropertyChanged and this is just a mistake which would be corrected if this were developed from a code example to an application.

Should ViewModel know about model?

Models are just the plain data, and a ViewModel is something that acts like a padding in between the two, that it should get information from the Model and pass it onto the View, and the View should know how to present it.


1 Answers

I actually ran into this exact same issue in a project I'm currently working on. As much as I wasn't a fan of it, I ended up doing the left to right approach and manually mapping my viewmodel data back to my entity.

The only nice thing about this approach is it does give you more control. Since I started using more compound viewmodels, where you actually have fields from more than one entity in your viewmodel, it started making more sense to do things this way.

I'm also using AutoMapper, and you're absolutely right, it does get awkward when you're trying to do a simple update operation. Wish I had some super clever workaround for you, but the "old fashioned way" seems to get the job done best for the work I've been doing.

like image 96
JasonOffutt Avatar answered Oct 12 '22 17:10

JasonOffutt