Model Binding
Hello Guys,
We will talk about ASP.NET MVC Model Binding mechanism.
Model Binding: It is the process that transform data come From View to Controller action parameters. If an Action Method has a paramether and if any requests come into our method before request comes into this method it goes to default Model Binder. Model Binder transform values comes from request to our suitable Model. In this way we can access values by using our Model. We can create our Model Binder and we can use it instead of default Model Binder. We will write it in the end.
Let’s make an example to explain Model Binding more.
As you see in the above image we are not doing model binding process because RmaUpdate method does not have any parameters. In this case we access data comes from View by using Request property. It is a Controller Class’s property and it allow us to access parameters that comes from View to our Action Method. We use it like => Request[“PropertyName”]. So we will always access values by using Request[“PropertyName”] ? No we won’t. It is wasting your time. Right here we have MVC Model Binding. It does handle tranforming proces for us. Let’s focus on below our Model Binding example.
When we send data with post method we will be able to access comingId value in RmaUpdate method. But you should notice data that are sent from chstml must have name attribute and the name must be the same in RmaUpdate method. Model Binder works with name parameter. If it is not same It won’t match our Model and we won’t be able to access comingId variable because it will be null. Be sure that you have same names because you won’t get any errors if it doesn’t.
I use Html helper to create TextBox and CheckBox before that I wrote using block and inside that block I created a form that has actionName, controllerName and also FormMethod type which is post here. If we want to get these parameters from TextBox and CheckBox we must type the names correct inside of Action method.
For my TextBox I wrote Recep Mert ASLAN and CheckBox was checked as you see I am able to access them in Index Method. If you would like to check your parameters just use break point then you will see it. If you change TextBox name in cshtml you will get null value in Index method. Be sure you type them correct.
Let’s go on with complex types such as Class. Let’s see how it works with complex type.
Get Recep Mert ASLAN’s stories in your inbox
Join Medium for free to get updates from this writer.
First I will create a class in Model folder and name it Supplier. Type some parameter inside that class.
Add a HomeController and define two methods Index and Update. Add View for Index. In index.cshtml we have a model which is Supplier. Type that model to your index.cshtml then write the following codes.
We created a form and we say we will send this form with post method. We created Labels and TextBoxes to allow people write their values. This form will go to Update which is an Action Method Name and it is in HomeController. Let’s go back to HomeController give a paramater for Update method. This parameter will be class here. Use break point to see values when we post it. Open your index.cshtml in your browser and type your values inside the textboxes and click rhe send button. In the next image you can see my parameters.
Model Binder did its task very well. What about if we want to do something else for our class something different. Let’s say I would like to see Title name next to ContactName how we will do that ? We will do that by creating our Model Binder. As I said in this writing beginning we will create our Model Binder to do that.
Let’s start a creating Folder and named ModelBinders then add new class named MyModelBinder and have it implement IModelBinder. There are 2 IModelBinders. You should choose the using name ending with Sytem.Web.Mvc. Now Our BindModel method was implemented. Fill that method like I did. In ControllerContext we have HttpContext and in HttpContext we have Request property to get values before bind them to action method. In here to get that values we will use Request[“PropertyName”] again. Get the values as I did then set that parameters to our Supplier and return it. In my case SupplierContactName will include SupplierTitleName. Note you do not have to use ToString(). It is my habit. I must get rid of it as soon as possible :)
Then we have a few last step to use our ModelBinder. First go to Global.asax and add new model. Just write the following code.
Now we have Update Method. We will write an attribute for our method to say we have Supplier parameter and we would like to use our Model Binding for that parameter. Check the following image.
As you see I wrote some parameters and my SupploerContactName would be Recep Mert ASLAN Mr. That’s because of our Model Binder. It works very well. I hope you guy like my article. Until see you next time take care guys and and do not be dependent on the code…










