ASP. Vnext modelbinding

Source: Internet
Author: User

Model Binding

The Model binding (binding) is simply the assignment of the action parameter by traversing the data obtained by the Valueprovider (value provider).

In the course of this tutorial, you should have a basic understanding of model binding.

Defaultpropertybindingpredicateprovider

Defaultpropertybindingpredicateprovider: Provides an expression-based method to provide the included properties.

     Public classDefaultpropertybindingpredicateprovider<tmodel>: IpropertybindingpredicateproviderwhereTModel:class        {         PublicDefaultpropertybindingpredicateprovider ();         Public Virtual stringPrefix {Get; }          Public VirtualFunc<modelbindingcontext,string,BOOL> PropertyFilter {Get; }          Public VirtualIenumerable<expression<func<tmodel,Object>>> Propertyincludeexpressions {Get; } }

This class has three properties:

Prefix represents the prefix name
PropertyFilter: Attribute Filtering
Propertyincludeexpressions: properties include expressions
To see how to use the instance code
     Private classExcludeuserpropertiesatparameter:defaultpropertybindingpredicateprovider<user>{Func<modelbindingcontext,string,BOOL> _defaultfilter1 =(context, PropertyName)=false;  PublicExcludeuserpropertiesatparameter () {} Public Override stringPrefix {Get                {                    return "User"; }            }                        Public OverrideIenumerable<expression<func<user,Object>>>propertyincludeexpressions {Get                {                                    yield returnm =M.registerationmonth; yield returnm =M.username; }            }        }
Controller
   Public class Bindattributecontroller:controller    {        public User echouser ([Bind (typeof(excludeuserpropertiesatparameter))] User User)        {            return  user;        }

User entity

  Public classUser {[Required] Public intId {Get;Set; }  Public intKey {Get;Set; } [Required] Public stringRegisterationmonth {Get;Set; }  Public stringUserName {Get;Set; }  PublicAddress Address {Get;Set; } }

The Prefix prefix name is user, which means that the request ID must be prefixed with the user
Propertyincludeexpressions: attributes include:
Get
{yield return m = m.registerationmonth; yield return m = = m.username;} 
Representation: This property of the detachment is assigned a value.
Let's look at the PropertyFilter effect.
   Private classExcludeuserpropertiesatparameter:defaultpropertybindingpredicateprovider<user>{Func<modelbindingcontext,string,BOOL> _defaultfilter1 =(context, PropertyName)=false;  PublicExcludeuserpropertiesatparameter () {} Public Override stringPrefix {Get                {                    return "User"; }            }             Public OverrideFunc<modelbindingcontext,string,BOOL>PropertyFilter {Get                    {                    return_defaultfilter1; }                }      }
_defaultfilter1 expression always returns false

All are not assigned,
PropertyFilter:
Propertyincludeexpressions equals null PropertyFilter will return true by default.

Fromqueryattribute
Fromrouteattribute
Fromformattribute

ASP. Vnext modelbinding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.