Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Usually the Model Binder (in MVC) looks at you Action method and sees what it requires (as in, the objects types). It then tries to find the values from the HTTP Request (values in the HTTP Form, QueryString, Json and maybe other places such as cookies etc. using ValueProviders). It then creates a new object with the parameters that it retrieves.</p> <p>IMO What you've done is not really "model binding". In the sense that you've just read the id and fetched the object from the DB.</p> <p>example of usual model binding:</p> <pre><code>// class public class SomeClass { public int PropA {get;set;} public string PropB {get;set;} } // action public ActionResult AddSomeClass(SomeClass classToBind) { // implementation } // pseudo html &lt;form action=""&gt; &lt;input name="PropA" type="text" /&gt; &lt;input name="PropB" type="text" /&gt; &lt;/form&gt; </code></pre> <p>if you post a form that contains the correct values (lets say you post a form with PropA and PropB ) the model binder can identify that you've sent those values in the form and build a SomeClass object.</p> <p>If you really want to create a real working example you should use a strongly typed View and use HtmlHelper's EditorFor (or EditorForModel) to create all the correct names that MVC needs.</p> <p>--</p> <p>for reference MVC's default binder is the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.aspx" rel="noreferrer">DefaultModelBinder</a>, and some (there are more, you can look around in the System.Web.Mvc namespace) ValueProviders that it uses by default are the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.formvalueprovider.aspx" rel="noreferrer">FormValueProvider</a> and the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.querystringvalueprovider.aspx" rel="noreferrer">QueryStringValueProvider</a></p> <p>So, as I already said, how this basically works is that the default model binder reads the model that the action is recieving (say SomeClass in the example) reads what are the values that it can read (say PropA and PropB) and asks the ValueProviders for the correct values for the properties.</p> <p>Also, if I recall correctly, you can also see the value providers in runtime using the ValueProviderFactories static class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload