Upload
learningtech
View
1.205
Download
0
Tags:
Embed Size (px)
Citation preview
ASP.NET MVC Model BinderJason
Class map
Agenda
Default Model Binder FormCollection Model Binder HttpPostedFileBase Model Binder Binding Attribute Custom Model Binder
Model Binder
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.
Binding To Primitive Values
Controller:
View:
Binding To Simple Objects
Binding To Collection Of Objects
Controller:
View:
FormCollection Model Binder
UpdateModel Syntax
example
Formcollection Model Binder
UpdateModel Syntax
example
HttpPostedFileBase Model Binder
View:
Controller:
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.
Binding Attribute
Custom Model Binder
Step1 You bind your custom object by implementing
the IModelBinder interface.
Custom Model Binder
Step2 After creating your binder, you need to register
the binder. In Application_Start().
Custom Model Binder
PersonModelBinder
Submit
Binding
Custom Model Binder
The first is by decorating the model class
Custom Model Binder
The second is decorating the parameter of your Action Method directly:
Summary
Easy Flexible Customize
Q & A
Reference
ASP.NET MVC4 http://
www.amazon.com/ASP-NET-MVC-Action-Jeffrey-Palermo/dp/1617290416
MSDN http://msdn.microsoft.com/zh-tw/library/system.web.mvc.de
faultmodelbinder(v=vs.108).aspx
Code Project http://
www.codeproject.com/Articles/551576/ASP-NET-MVC-Model-Binding-and-Data-Annotation