现在我的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; }
}
}
答案 0 :(得分:0)
作为Include的替代方法,您可以使用Exclude。它只会排除您不想发布的属性。可能在大多数情况下它会更少,或者如果你想要,你可以删除包含和排除,并且所有数据都将被发布。
例如:
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit([Bind(Exclude = "GeneralContractor")] Project project)
{
}
更多/实用信息:http://csharp-video-tutorials.blogspot.in/2013/05/part-21-including-and-excluding.html