ASP.NET MVC - 模型绑定 - System.Web.Mvc.BindAttribute

创建时间:
2018-09-18 11:02
最近更新:
2018-09-18 11:02

Resource - MSDN

  1. BindAttribute Class - Represents an attribute that is used to provide details about how model binding to a parameter should occur.

Example

以下是 BindAttribute 的全部用法的示例:

//标注 Action:
public ActionResult Register([Bind(Include="FirstName,  LastName")] Person person) {...} //仅包含所列属性
public ActionResult Register([Bind(Exclude="IsApproved, Role"    )] Person person) {...} //仅排除所列属性

//标注 Controller:
[Bind(Exclude = "IsApproved")]
public class Person { ... }

//标注 Prefix:
[BindAttribute(Prefix="p")]

Source Code of System.Web.Mvc.BindAttribute from JetBrains dotPeek 1.0 at 2015-10-27

Complete Copy:

// Type: System.Web.Mvc.BindAttribute
// Assembly: System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages\Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll

using System;
using System.Collections.Generic;
using System.Linq;

namespace System.Web.Mvc
{
  /// <summary>
  /// Represents an attribute that is used to provide details about how model binding to a parameter should occur.
  /// </summary>
  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
  public sealed class BindAttribute : Attribute
  {
    private string[] _excludeSplit = new string[0];
    private string[] _includeSplit = new string[0];
    private string _exclude;
    private string _include;

    /// <summary>
    /// Gets or sets a comma-delimited list of property names for which binding is not allowed.
    /// </summary>
    /// 
    /// <returns>
    /// The exclude list.
    /// </returns>
    public string Exclude
    {
      get
      {
        return this._exclude ?? string.Empty;
      }
      set
      {
        this._exclude = value;
        this._excludeSplit = AuthorizeAttribute.SplitString(value);
      }
    }

    /// <summary>
    /// Gets or sets a comma-delimited list of property names for which binding is allowed.
    /// </summary>
    /// 
    /// <returns>
    /// The include list.
    /// </returns>
    public string Include
    {
      get
      {
        return this._include ?? string.Empty;
      }
      set
      {
        this._include = value;
        this._includeSplit = AuthorizeAttribute.SplitString(value);
      }
    }

    /// <summary>
    /// Gets or sets the prefix to use when markup is rendered for binding to an action argument or to a model property.
    /// </summary>
    /// 
    /// <returns>
    /// The prefix to use.
    /// </returns>
    public string Prefix { get; set; }

    internal static bool IsPropertyAllowed(string propertyName, ICollection<string> includeProperties, ICollection<string> excludeProperties)
    {
      bool flag1 = includeProperties == null || includeProperties.Count == 0 || Enumerable.Contains<string>((IEnumerable<string>) includeProperties, propertyName, (IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase);
      bool flag2 = excludeProperties != null && Enumerable.Contains<string>((IEnumerable<string>) excludeProperties, propertyName, (IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase);
      if (flag1)
        return !flag2;
      else
        return false;
    }

    /// <summary>
    /// Determines whether the specified property is allowed.
    /// </summary>
    /// 
    /// <returns>
    /// true if the specified property is allowed; otherwise, false.
    /// </returns>
    /// <param name="propertyName">The name of the property.</param>
    public bool IsPropertyAllowed(string propertyName)
    {
      return BindAttribute.IsPropertyAllowed(propertyName, (ICollection<string>) this._includeSplit, (ICollection<string>) this._excludeSplit);
    }
  }
}