Your SlideShare is downloading. ×
05 model binder
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

05 model binder

253

Published on

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

  • Be the first to like this

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Model Binder
  • 2. Remember, the browser Model calls the controller and has to get data to it. Controller ViewBrowser
  • 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. MVC provides two ways toread data in the controlleractions1.  FormCollection2.  Model binder
  • 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. 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. Model binder to the rescue!1.  Form dataRequest.Form["FirstName"]2.  Routing dataRouteData["FirstName"]3.  QueryStringRequest.QueryString["FirstName"]
  • 8. Model binding – form data›  In form:<input type="text" name="FirstName" /><input type="text" name="LastName" />
  • 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. 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. 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

×