Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM and ModelBinders in the ASP.NET MVC Framework

I've got a series of views, each are typed to have their own ViewModel class which contains everything they need to display themselves, for example:

public class CreateResourceViewModel
{
     public Project Parent { get; set; }
     public SelectList Categories { get; set; }
     public Resource Resource { get; set; }
}

The post action method for this I'd like to use would look like this:

[AcceptVerbs (HttpVerbs.Post)]
public ActionResult Create (Resource resource)
{
   // Update code...
}

Notice that the only object I'm interested in is the Resource property of the CreateResourceViewModel, not the CreateResourceViewModel itself. Everything else is just gravy for for the user, what they're updating is the resource class...

Is this possible within the MVC Framework (even if it's v2 CTP)?

Thanks all

like image 500
Kieron Avatar asked Aug 14 '09 11:08

Kieron


People also ask

What is MVVM in ASP.NET MVC?

MVVM stands for Model-View-View Model. This pattern supports two-way data binding between view and View model. This enables automatic propagation of changes, within the state of view model to the View. Typically, the view model uses the observer pattern to notify changes in the view model to model.

Is ASP.NET MVC or MVVM?

The MVC pattern is most popular and is used for ASP.NET, iOS, and Android development. The MVP pattern is mostly used for ASP.NET Web Forms applications, and MVVM is used for WPF and Silverlight development.

What is the difference between model and ViewModel in MVC?

A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.

How will you implement MVVM and MVC?

The key to MVVM in MVC is moving logic out of the MVC controller and into a view model class that contains properties that you can bind to the user interface. The controller should only call a method in your view model, and periodically set a property or two prior to calling that method.


1 Answers

Sure. Use:

 public ActionResult Create([Bind(Prefix="Resource")]Resource resource)
like image 74
Craig Stuntz Avatar answered Sep 21 '22 11:09

Craig Stuntz