オブジェクトに関連するフォーム + プロパティに関連するオブジェクトのリストが必要です
ex : 名前と各項目 x : 有効かどうか / 価格
注:アイテムの数はわかっていますが、別のテーブルに関連しています。すべてのデータはユーザーが編集できます。
ASP .Net MVC でこれを達成するにはどうすればよいですか?
public class AddChargeModel
{
[Required]
[Display(Name = "Name")]
public string Nom { get; set; }
[Required]
public List<ChargeLotModel> ChargeLot;
}
public class ChargeLotModel
{
[Required]
[Display(Name = "IdLot")]
public int IdLot { get; set; }
[Display(Name = "Price")]
public decimal Price { get; set; }
[Display(Name = "Active")]
public bool Active { get; set; }
}
クラス AddChargeModel をビューに関連付けます。
@model Models.AddChargeModel
@using (Html.BeginForm())
{
<label>@Html.LabelFor(m => m.Name)</label>
@Html.EditorFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
<table>
<tr>
<th>Price</th>
<th>Active</th>
</tr>
@for (var lotindex = 0; lotindex < ViewData.Model.ChargeLot.Count(); lotindex++)
{
<tr>
<td>@Html.EditorFor(model => model.ChargeLot[lotindex].Price) @Html.HiddenFor(model => model.ChargeLot[lotindex].IdLot)</td>
<td>@Html.EditorFor(model => model.ChargeLot[lotindex].Active)</td>
</tr>
}
</table>
<input type="submit" value="Valider" class="button" />
}
ボタンをクリックすると、コントローラー関数に入ります。
[HttpPost]
public ActionResult Add(AddChargeModel model)
{
...
}
model.Name は入力されていますが、model.ChargeLot == null ではありません。
配列内のコントロールは、Web ページの ChargeLot_0__Price のように名前が付けられています。(そして、私がよく理解していればうまくいくはずです)
それを機能させるための解決策はありますか?