ASP.NET MVC Model binding is one of the best features in ASP.NET MVC Framework. It is a bridge between HTTP request and action methods. It helps GET and POST to get automatically transferred into the data model.
MVC Model Binder uses Default Model Binder for building model parameter for MVC runtime.
Now, before this we would like to first explain simple model binding.
Now, we have created a new MVC Web application project by File- New Project- Web Application – MVC.
Now, we will add a new strongly typed View which we will bind to the model, as shown below.
@model ModelBinding.Models.Client
@ {
ViewBag.Title = "GetClient";
} < h3 Details < /h3>
< h2 >
Cleint Code: @Model.ClientCode
< br / >
Cleint Name: @Model.ClientName
< /h2>
Now, add a Client Model in Models folder, as shown below.
public class Client {
public string ClientCode {
get;
set;
}
public string ClientName {
get;
set;
}
}
Add ClientController which is shown below.
public class ClientController: Controller {
// GET: Client
public ActionResult GetClient() {
Client cnt = new Client {
ClientCode = "1001",
ClientName = "Peter"
};
return View(cnt);
}
}
Now, add another action method as EnterClient which will allow the users to enter client details, as shown below.
public ActionResult EnterCleint() {
return View();
}
As shown below, it is having two textboxes and Submit button. Along with it, the form is using POST method for the action Submit.
@ {
ViewBag.Title = "EnterCleint";
} < h2 > EnterCleint < /h2> < form action = "Submit"
method = "post" > Cleint Code: < input type = "text"
name = "ClientCode " / > < br / > Cleint Name: < input type = "text"
name = "ClientName " / > < br / > < input type = "submit"
value = "Submit"
id = "Submit" / > < /form>
Now, we will create Submit action methond in the Controller.
public ActionResult Submit() {
Client cnt = new Client {
ClientCode = Request.Form["CleintCode"],
ClientName = Request.Form["CleintName"]
};
return View("GetClient", cnt);
}
In the above code, we are fetching the values on the basis of textbox name and assigning it to Client and passing to GetCleint View.
Now, you can improve your code by adding parameter of type model in action method as shown below,
public ActionResult Submit(Client cnt) {
return View("GetClient", cnt);
}
Here, we have to take care textboxes name must be same in above example.
Now, if we will take a real-life example - the designing team works on design and developers work on development i.e. property name of the model and control names are different. In this case, it is very difficult to maintain the name on both sides or we can say lots of efforts is required for the same.
To overcome such a scenario, we can take the help of Model Binders, we can say Model Binder has mapping code which connects your user interface with your Model. It acts like a bridge between the Model and View.
Now, let’s see the same with example.
Lets add a View having two textboxes and Submit button. Along with it, the form is using post method to action Submit.
@ {
ViewBag.Title = "EnterCleint";
} < h2 > EnterCleint < /h2> < form action = "Submit"
method = "post" > Cleint Code: < input type = "text"
name = "txtClientCode" / > < br / > Cleint Name: < input type = "text"
name = "txtClientName" / > < br / > < input type = "submit"
value = "Submit"
id = "Submit" / > < /form>
In the above example, the View is having two textboxes, i.e., txtClientCode and txtClientName which are not same as property names of Client model.
Now, add a new folder as Utilities.
Now, add a class, ClientBinder class, which will implement the System.Web.Mvc.IModelBinder as shown below.
public class ClientBinder: IModelBinder {
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
HttpContextBase context = controllerContext.HttpContext;
string clientCode = context.Request.Form["txtClientCode"];
string clientName = context.Request.Form["txtClientName"];
return new Client {
ClientCode = clientCode,
ClientName = clientName
};
}
}
In the above code, we are accessing the form object from the request object with the help of context object and converting it into Model, i.e., Client.
Now, we need to use the above ClientBinder and map it to our Client object which we will do in below.
ActionResult Submit([ModelBinder(typeof(ClientBinder))] Client cnt)
In the above code, we are using ClientBinder.
Now, add/modify the action method.
public ActionResult Submit([ModelBinder(typeof(ClientBinder))] Client cnt) {
return View("GetClient", cnt);
}
That's it. We are done here.