05 model binder
Upcoming SlideShare
Loading in...5
×
 

05 model binder

on

  • 599 views

 

Statistics

Views

Total Views
599
Views on SlideShare
599
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

05 model binder 05 model binder Presentation Transcript

  • Model Binder
  • Remember, the browser Model calls the controller and has to get data to it. Controller ViewBrowser
  • 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>
  • 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){ string firstName = collection["FirstName"]; string lastName = collection["LastName"]; string theKey = collection["SecretKey"]; …}
  • Consider this situation ...›  In controller:public ActionResult Update(string FirstName, string LastName){ // Values are magically populated! string fullName= FirstName + " " + LastName; ...}›  It just happens! How? …
  • Model binder to the rescue!1.  Form dataRequest.Form["FirstName"]2.  Routing dataRouteData["FirstName"]3.  QueryStringRequest.QueryString["FirstName"]
  • 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›  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”
  • 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; ...}
  • 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