Как использовать атрибут [Bind (Include = «»)] для сложных вложенных объектов ?

Я создаю список замков, каждый замок имеет серийный номер (Title), связанную школу (SchoolCode) и 5 ​​связанных комбинаций (с Number, Combination и IsActive). Мы используем Ncommon и linq и настроили их как вложенные сущности (Lock Has Many Combinations).

В форме я использую шаблоны 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>

Когда я запускаю это без атрибута Bind, привязка модели работает нормально. Как только я добавляю привязку, мне кажется, что она не может быть привязана ни к одной из комбинаций.

[HttpPost]
public ActionResult Add([Bind(Include = "SchoolCode,Title,Combinations.Combination,Combination.Number,Combinations[2].Combination")] LockerLock @lock, [Range(1, 5)] int ActiveCombination)
{
...
}
15
задан Lucent Fox 18 July 2011 в 21:35
поделиться