繁体   English   中英

控制器动作的绑定模型的替代方法

[英]Alternatives to binding model for controller action

现在,我的Create和Edit POST操作具有相当长的函数定义,因为有很多变量,而bind属性包含所有变量。

public ActionResult Create([Bind(Include = "ProjectName,ProjectDescription,DateReceived,EffectiveDate,ExpirationDate,GeneralContractor,ProjectTerm,ProjectType,SubmissionNumber,PolicyNumber,Status,Underwriter,Division,BrokerCity,TAName,Branch,FirstNamedInsuredAddress,FirstNamedInsured,ProjectAddress")] Project project) {

public ActionResult Edit([Bind(Include = "ProjectID,ProjectName,ProjectDescription,DateReceived,EffectiveDate,ExpirationDate,GeneralContractor,ProjectTerm,ProjectType,SubmissionNumber,PolicyNumber,Status,Underwriter,Division,BrokerCity,TAName,Branch,FirstNamedInsuredAddress,FirstNamedInsured,ProjectAddress")] Project project) {

是否有替代方法可以使定义更短?

这是我的模型:

public class Project
{
    public Project()
    {
        FirstNamedInsuredAddress = new Address();
        ProjectAddress = new Address();
    }

    [Key]
    public int ProjectID { get; set; }

    [Required]
    [StringLength(150)]
    [Display(Name = "Project Name")]
    public string ProjectName { get; set; }

    [StringLength(1000)]
    [Display(Name = "Project Description")]
    public string ProjectDescription { get; set; }

    [Display(Name = "Date Received")]
    public string DateReceived { get; set; }

    [Display(Name = "Effective Date")]
    public string EffectiveDate { get; set; }

    [Display(Name = "Expiration Date")]
    public string ExpirationDate { get; set; }

    [StringLength(150)]
    [Display(Name = "General Contractor")]
    public string GeneralContractor { get; set; }

    [StringLength(25)]
    [Display(Name = "Project Term")]
    public string ProjectTerm { get; set; }

    [Display(Name = "Project Type")]
    public string ProjectType { get; set; }

    [Required]
    [Display(Name = "Submission Number")]
    public long SubmissionNumber { get; set; }

    [StringLength(20)]
    [Display(Name = "Policy Number")]
    public string PolicyNumber { get; set; }

    public string Status { get; set; }

    [StringLength(100)]
    public string Underwriter { get; set; }

    public string Division { get; set; }

    [StringLength(50)]
    [Display(Name = "Broker/City")]
    public string BrokerCity { get; set; }

    [StringLength(100)]
    [Display(Name="TA Name")]
    public string TAName { get; set; }

    public string Branch { get; set; }

    [Display(Name="First Named Insured Address")]
    public Address FirstNamedInsuredAddress { get; set; }

    [StringLength(150)]
    [Display(Name="First Named Insured")]
    public string FirstNamedInsured { get; set; }

    [Display(Name="Project Address")]
    public Address ProjectAddress { get; set; }

    public class Address
    {
        [StringLength(150)]
        [Display(Name="Line 1")]
        public string Line1 { get; set; }

        [StringLength(150)]
        [Display(Name="Line 2")]
        public string Line2 { get; set; }

        [StringLength(100)]
        public string City { get; set; }

        public string State { get; set; }

        [Display(Name="Zip Code")]
        public int? ZipCode { get; set; }

        [StringLength(100)]
        public string County { get; set; }

    }
}

作为Include的替代方法,您可以使用Exclude。 它只会排除您不想发布的属性。 在大多数情况下,它可能会减少,或者如果您愿意,可以将include和exclude都删除,所有数据都将发布。

例如:

[HttpPost]
[ActionName("Edit")]
public ActionResult Edit([Bind(Exclude = "GeneralContractor")] Project project)
{

}

更多/有用的信息: http : //csharp-video-tutorials.blogspot.in/2013/05/part-21-includes-and- included.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM