What is model binding in ASP.NET MVC, Why is it needed? Can someone give simple example , Can model binding be achieved by checking create strongly typed view?
Asked
Active
Viewed 2.1k times
13
-
Check this: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application – Maxim Zhukov Jul 18 '13 at 10:58
-
It is needed for seperation of concerns and to make things more OOP way. – DarthVader Jul 18 '13 at 11:14
-
possible duplicate of [ASP.NET MVC 3 Model Binding Resources](http://stackoverflow.com/questions/5692964/asp-net-mvc-3-model-binding-resources) – CodeCaster Jul 18 '13 at 12:02
1 Answers
21
ModelBinding
is the mechanism ASP.NET MVC uses to create strongly-typed objects (or fill primitive-type parameters) from the input stream (usually an HTTP request).
For example, consider this Person
model:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Now, you have some Action
in some Controller
that's expecting a Person
type as a parameter:
public class HomeController : Controller
{
public ActionResult EditPersonDetails(Person person)
{
// ...
}
}
The Model-Binder
is then responsible to fill that person
parameter for you. By default it does it by consulting the ValueProviders
collection and asking for the value of each property in the (to be bound) model.
More on Value-Providers and Model-Binders on http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx/

haim770
- 48,394
- 7
- 105
- 133