1

グリッド用に次の 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
4

1 に答える 1

0

この投稿はまさにあなたが望むものです:http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

MVC 3のデフォルトのモデルバインダーは、アイテムのキーと値を指定する辞書をバインドするため、この場合は次のようになります。

<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[0].Key" value='Test' />
            <input type='text' name="Prize[0].Properties[0].Value.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[0].Properties[0].Key" value='Test' />
            <input type='text' name="Prize[0].Properties[0].Value.Name" value='Failed' />
       </td>
     </tr>
   </tbody>
 </table>
 <input type="submit"/>
</form>

ただし、実装する内容や方法によっては、キーを非表示にすることができます。

于 2013-03-15T01:45:46.880 に答える