Model binding

432 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
432
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Model binding

  1. 1. Jason ASP.NET MVC Model Binder
  2. 2. Class map
  3. 3. Agenda Default Model Binder  FormCollection Model Binder  HttpPostedFileBase Model Binder  Binding Attribute  Custom Model Binder 
  4. 4. Model Binder
  5. 5. Default Model Binder  The DefaultModelBinder class maps the following types of objects to a browser request.  Primitive types, such as String , Double, Decimal , or DateTime objects.  Collections, such as ICollection<T>, IList<T>, or IDictionary<TKey, TValue>.  Model classes, such as Person, Address, or Product.
  6. 6. Binding To Primitive Values  Controller:  View:
  7. 7. Binding To Simple Objects
  8. 8. Binding To Collection Of Objects  Controller:  View:
  9. 9. FormCollection Model Binder  UpdateModel Syntax  example
  10. 10. Formcollection Model Binder  UpdateModel Syntax  example
  11. 11. HttpPostedFileBase Model Binder  View:  Controller:
  12. 12. Binding Attribute  Include   Gets or sets a comma-delimited list of property names for which binding is allowed. Exclude  Gets or sets a comma-delimited list of property names for which binding is not allowed.
  13. 13. Binding Attribute
  14. 14. Custom Model Binder  Step1  You bind your custom object by implementing the IModelBinder interface.
  15. 15. Custom Model Binder  Step2  After creating your binder, you need to register the binder. In Application_Start().
  16. 16. Custom Model Binder Submit PersonModelBinder Binding
  17. 17. Custom Model Binder  The first is by decorating the model class
  18. 18. Custom Model Binder  The second is decorating the parameter of your Action Method directly:
  19. 19. Summary    Easy Flexible Customize
  20. 20. Q&A
  21. 21. Reference  ASP.NET MVC4   MSDN   http://www.amazon.com/ASP-NET-MVC-Action-JeffreyPalermo/dp/1617290416 http://msdn.microsoft.com/zhtw/library/system.web.mvc.defaultmodelbinder(v=vs.108).aspx Code Project  http://www.codeproject.com/Articles/551576/ASP-NET-MVC-Model-Binding-and-DataAnnotation

×