Model Binder
Remember, the browser                            Model calls the controller and has to get data to it.              Contro...
The data is often sent as part of aform›  In the view:<form action="/Person/Update">    <input type="text" name="FirstNam...
MVC provides two ways toread data in the controlleractions1.    FormCollection2.    Model binder
FormCollection›  A  dictionary of strings›  In Controller action:Public ActionResult Update(FormCollection collection){ ...
Consider this situation ...›  In controller:public ActionResult Update(string FirstName,                             stri...
Model binder to the rescue!1.  Form dataRequest.Form["FirstName"]2.    Routing dataRouteData["FirstName"]3.  QueryStringRe...
Model binding – form data›  In form:<input type="text" name="FirstName" /><input type="text" name="LastName" />
Model binding - Routing data›  In url:http://www.tic.com/Person/Update/35›  The 35 is mapped to a parameter called Id› ...
Model Binding - QueryString›  In URL:http://www.tic.com/Person/Update?FirstName=Leonard&LastName=Hofstadter›  In control...
Conclusion›  State management has to be done    because the web is a stateless model›  We move data from the controller ...
Upcoming SlideShare
Loading in...5
×

05 model binder

267

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
267
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

05 model binder

  1. 1. Model Binder
  2. 2. Remember, the browser Model calls the controller and has to get data to it. Controller ViewBrowser
  3. 3. The data is often sent as part of aform›  In the view:<form action="/Person/Update"> <input type="text" name="FirstName" /> <input type="text" name="LastName" /> <input type="hidden" name="SecretKey" /></form>
  4. 4. MVC provides two ways toread data in the controlleractions1.  FormCollection2.  Model binder
  5. 5. FormCollection›  A dictionary of strings›  In Controller action:Public ActionResult Update(FormCollection collection){ string firstName = collection["FirstName"]; string lastName = collection["LastName"]; string theKey = collection["SecretKey"]; …}
  6. 6. Consider this situation ...›  In controller:public ActionResult Update(string FirstName, string LastName){ // Values are magically populated! string fullName= FirstName + " " + LastName; ...}›  It just happens! How? …
  7. 7. Model binder to the rescue!1.  Form dataRequest.Form["FirstName"]2.  Routing dataRouteData["FirstName"]3.  QueryStringRequest.QueryString["FirstName"]
  8. 8. Model binding – form data›  In form:<input type="text" name="FirstName" /><input type="text" name="LastName" />
  9. 9. Model binding - Routing data›  In url:http://www.tic.com/Person/Update/35›  The 35 is mapped to a parameter called Id›  Or with custom routing:http://www.tic.com/Person/Cooper/Sheldon›  The “Cooper” could be mapped to “LastName” and “Sheldon” could be mapped to “FirstName”
  10. 10. Model Binding - QueryString›  In URL:http://www.tic.com/Person/Update?FirstName=Leonard&LastName=Hofstadter›  In controller:public ActionResult Update(string FirstName, string LastName){ string fullName= FirstName + " " + LastName; ...}
  11. 11. Conclusion›  State management has to be done because the web is a stateless model›  We move data from the controller to the browser with ViewBag, Strongly-typed views, and ViewModels›  We get data from the browser to the controller using form data, routing data, or QueryString
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×