Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC Custom Model Binding explanation [closed]

Tags:

asp.net-mvc

Is anyone aware of where I can find a tutorial for building a custom model binder.

I have found several issues using the default model binder with EntitySets (not populating when creating new objects and creating duplicates on editing objects) and find that my UI doesn't match my model exactly (e.g. time fields formated "hh:mm am" do not match with a timespan). I therefore want to look at whether creating a custom model binder is a better solution. I have found a couple of examples: here and here. The first is I believe out of date and the second doesn't really explain what is going on.

My main problem is that I don't fully understand what the ControllerContext and ModelBindingContext Of the BindModel method actually are, i.e. how they relate to the controller and model.

I would be grateful if someone point me to a tutorial or could take a look at the dave hayden's post and show me how it might look with the current implementation of MVC.

like image 392
Richbits Avatar asked Jul 28 '09 11:07

Richbits


People also ask

What is custom model binding in MVC?

Model binding in MVC maps HTTP request data to the parameters of the controller's action method. The parameter may either be of simple type like integers, strings, double etc. or complex types. MVC binds the request data to the action parameter by parameter name. ModelBinder.zip. Introduction.

How does MVC model binding work?

Model binding is a mechanism ASP.NET MVC uses to create parameter objects defined in controller action methods. The parameters can be of any type, from simple to complex ones. It simplifies working with data sent by the browser because data is automatically assigned to the specified model.

What is a correct use case for creating a custom model binder in asp net core?

You might need to transform the input prior to binding it. For example, when you have a key that can be used to look up model data. You can use a custom model binder to fetch data based on the key.


2 Answers

I've actually now found the following two posts which a good summary of model binding:

http://odetocode.com/Blogs/scott/archive/2009/04/27/12788.aspx

http://odetocode.com/Blogs/scott/archive/2009/05/05/12801.aspx

like image 105
Richbits Avatar answered Oct 12 '22 11:10

Richbits


Here's a good explanation of ModelBinders that I believe will cover your questions: http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx

A quick Google search pulled up a few others... I will let you filter through them :-)

like image 4
mkchandler Avatar answered Oct 12 '22 11:10

mkchandler