2017-06-23 19 views
0

今、私のPOSTアクションは、多くの変数があり、バインド属性にはすべてが含まれているため、作成と編集のためのPOSTアクションはかなり長い関数定義を持っています。コントローラアクション用のバインディングモデルの代替手段

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; } 

    } 
} 
+2

ええと、どちらか私は酔っていたり、単に '公共のActionResult編集(Projectプロジェクト)を行う'と、それはデフォルトではすべてをバインドさせることができます。 – PTwr

+0

@PTwr正確には、それはモデルの使用の全ポイントですか? – jamiedanq

+0

@jamiedanqここで私が思うのは、 'ViewModel'の' ViewModel'として 'Bind'を使用した場合、ID上の私の飲酒した目の斑点[Key]'が適切な 'ViewModel'の欠如の回避策として使われていると思います。 – PTwr

答えて

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