グリッド用に次の hml を生成しています。
<form method="POST" action="home/Edit">
<table>
<thead>
<tr>
<th> Level </th>
<th> Cost </th>
<th> Test </th>
</tr>
</thead>
<tbody>
<tr>
<td> <input type='text' name="Prize[0].Level" value='1' /> </td>
<td> <input type='text' name="Prize[0].Cost" value='$1.00' /> </td>
<td> <input type='text' name="Prize[0].Properties['Test'].Name" value='Passed' /> </td>
</tr>
<tr>
<td> <input type='text' name="Prize[1].Level" value='2' /> </td>
<td> <input type='text' name="Prize[1].Cost" value='$2.00' /> </td>
<td> <input type='text' name="Prize[1].Properties['Test'].Name" value='Failed' /> </td>
</tr>
</tbody>
</table>
<input type="submit"/>
</form>
私のホームコントローラーにはメソッドがあります:
[HttpPost]
public ActionResult Edit(PrizelevelsModel model) { ... }
そして、モデルは次のように定義されます。
public class PrizelevelsModel
{
public PrizeLevel[] Prize { get; set; }
}
public class PrizeLevel
{
public readonly Dictionary<string, PrizeLevelProperty> Properties = new Dictionary<string, PrizeLevelProperty>();
public int Level { get; set; }
public decimal Cost { get; set; }
}
public class PrizeLevelProperty
{
public int Ordinal { get; set; }
public string Name { get; set; }
public object Value { get; set; }
}
[送信] をクリックすると、MVC はモデルを Prize[2] で膨らませます。配列内の各 Prizelevel には正しい Level と Cost がありますが、Properties ディクショナリには 0 要素があります。MVC を各賞品のプロパティ ディクショナリに、キーが「Test」で、値が各賞品のフォームに設定された名前を持つ新しい PrizeLevelProperty である要素を追加するにはどうすればよいですか?
つまり、コントローラー メソッドをデバッグするときは、次のことを確認したいと考えています。
model.Prize[0].Level == 1 // this works ok
model.Prize[0].Cost == "$1.00" // and so does this
model.Prize[0].Properties["Test"].Name" == "Passed" // but not this, instead model.Prize[0].Properties.Count == 0