2011-07-18 35 views
13

我正在創建一個鎖的清單,每個鎖都有一個序列號(標題),一個關聯的學校(SchoolCode)和5個關聯的組合(具有Number,Combination和IsActive)。我們使用Ncommon和linq,並將它們設置爲嵌套實體(Lock Has Many Combinations)。如何在複雜的嵌套對象上使用[Bind(Include =「」)]屬性?

在窗體上,我使用JQuery模板動態構建表單。其中SchoolCode和Title是基本表單元素,Combinations [index] .Number和Combinations [index] .Combination是子元素。

<form method="post" action="/Lockers.aspx/Locks/Add">  
<input type="hidden" name="SchoolCode" value="102"> 
Lock S/N: <input type="text" name="Title" value=""><br>  
<div id="combinations"> 
<input type="hidden" name="Combinations[0].Number" value="1"> 
<input type="text" name="Combinations[0].Combination" value=""> 
<input type="radio" value="1" name="ActiveCombination"><br> 
<input type="hidden" name="Combinations[1].Number" value="2"> 
<input type="text" name="Combinations[1].Combination" value=""> 
<input type="radio" value="2" name="ActiveCombination"><br> 
<input type="hidden" name="Combinations[2].Number" value="3"> 
<input type="text" name="Combinations[2].Combination" value=""> 
<input type="radio" value="3" name="ActiveCombination"><br> 
<input type="hidden" name="Combinations[3].Number" value="4"> 
<input type="text" name="Combinations[3].Combination" value=""> 
<input type="radio" value="4" name="ActiveCombination"><br> 
<input type="hidden" name="Combinations[4].Number" value="5"> 
<input type="text" name="Combinations[4].Combination" value=""> 
    <input type="radio" value="5" name="ActiveCombination"><br></div> 
    <input type="submit" id="add" value="Add »"> <br> 
</form> 

當我運行這個沒有綁定屬性時,模型綁定工作正常。一旦我添加綁定,我似乎無法將其綁定到任何組合。

[HttpPost] 
public ActionResult Add([Bind(Include = "SchoolCode,Title,Combinations.Combination,Combination.Number,Combinations[2].Combination")] LockerLock @lock, [Range(1, 5)] int ActiveCombination) 
{ 
... 
} 

回答

6

從我可以告訴我需要告訴它綁定到一個名爲組合鎖的特性,從那裏我不能再選擇包含或排除屬性的子對象的綁定。相反,我需要在域模型對象本身指定綁定屬性。

[HttpPost] 
public ActionResult Add([Bind(Include = "SchoolCode,Title,Combinations")] LockerLock @lock, [Range(1, 5)] int ActiveCombination) 
{ 
... 
} 

則綁定屬性包括組合對象...

[Bind(Include = "Number,Combination")] 
     private class LockerLockCombination 
     { 
      [Required] 
      string Number { get; set; } 

      [Required] 
      string SchoolCode { get; set; } 
     } 

爲了保持一致性,我可能就包括對初始鎖模型綁定...

只是爲了對比,這是我的最終解決方案。在這兩種情況下,我只是將BindAttribute添加到域模型中:

namespace Project.Web.Models 
{ 
    [MetadataType(typeof(LockerLock.Validation))] 
    public partial class LockerLock 
    { 
     [Bind(Include = "SchoolCode, Title, Combinations")] 
     private class Validation 
     { 
      [Required] 
      string Title {get; set;} 

      [Required] 
      string SchoolCode {get; set;} 
     } 

    } 
} 

namespace Project.Web.Models 
{ 
    [MetadataType(typeof(LockerLockCombination.Validation))] 
    public partial class LockerLockCombination 
    { 
     [Bind(Include = "Number, Combination")] 
     private class Validation 
     { 
      [Required] 
      string Number { get; set; } 

      [Required] 
      string Combination { get; set; } 
     } 

    } 
} 
相關問題