Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modelbinding database entities in ASPNET MVC

Tags:

I'm having trouble trying to think what the best way is to recreate a database object in a controller Action.

I want to make use of ModelBinders so in my action I have access to the object via a parameter, rather than having to repeat code to get an object from the database based on an identifier parameter. So I was thinking of having a ModelBinder that performs a call to the dataaccess layer to obtain the original object (or creates a new one if it doesn't exist in the database), then binds any properties to the database object to update it. However I've read that the ModelBinders shouldn't make database queries (first comment of this article).

If the ModelBinder shouldn't perform a database query (so just using the DefaultModelBinder) then what about database objects that have properties that are other db objects? These would never get assigned.

Saving an object after the user has edited it (1 or 2 properties are editable in the view) the ModelBinded object would be missing data, so saving it as it is would result in data in the database being overwritten with invalid values, or NOT-NULL constraints failing.

So, whats the best way to get an object in a controller action from the database bound with the form data posted back from the view?

Note im using NHibernate.

like image 873
Luke Smith Avatar asked Feb 05 '09 00:02

Luke Smith


People also ask

What are model binders in MVC?

Model binding is a well-designed bridge between the HTTP request and the C# action methods. It makes it easy for developers to work with data on forms (views), because POST and GET is automatically transferred into a data model you specify. ASP.NET MVC uses default binders to complete this behind the scene.

Is MVC two-way data binding?

So the two-way data binding means we can perform both read and write operations.

How data binding can be used in ASP.NET MVC?

ASP.NET MVC model binder allows you to map Http Request data with the model. Http Request data means when a user makes a request with form data from the browser to a Controller, at that time, Model binder works as a middleman to map the incoming HTTP request with Controller action method.

Does MVC use data binding?

MVC doesn't use data bindings like old web api. You have to use model bindings in a MVC or MVVM approach.


1 Answers

I get the model object from the database, then use UpdateModel (or TryUpdateModel) on the object to update values from the form parameters.

public ActionResult Update( int id )
{
     DataContext dc = new DataContext();
     MyModel model = dc.MyModels.Where( m => m.ID == id ).SingleOrDefault();

     string[] whitelist = new string[] { "Name", "Property1", "Property2" };

     if (!TryUpdateModel( model, whitelist )) {
        ... model error handling...
        return View("Edit");
     }

     ViewData.Model = model;

     return View("Show");
}
like image 88
tvanfosson Avatar answered Sep 18 '22 21:09

tvanfosson