モデルのバインドに問題がありました。次のモデルをここに取得しました:
public class RoomScan
{
public RoomScan() { }
public RoomScan(Guid id)
{
Room_ID = id;
Assets = new List<AssetScanModel>();
}
//public Guid Soll_ID { get; set; }
public Guid Room_ID { get; set; }
public List<AssetScanModel> Assets { get; set; }
[Display(Name = "Barcode", ResourceType = typeof(Dictionary))]
public string Barcode { get; set; }
[Display(Name = "RFID", ResourceType = typeof(Dictionary))]
public string RFID { get; set; }
}
public class AssetScanModel
{
public AssetScanModel(Asset asset)
{
Asset = asset;
Scanned = false;
CheckIn = false;
}
public Asset Asset { get; set; }
[Display(Name = "Scanned", ResourceType = typeof(Dictionary))]
public bool Scanned { get; set; }
[Display(Name = "CheckIn", ResourceType = typeof(Dictionary))]
public bool CheckIn { get; set; }
}
ビューにはすべてのアセットが一覧表示されます。
using (Html.BeginForm("Scan", "Inventory", FormMethod.Post))
{
Html.HiddenFor(rs => rs.Room_ID);
Html.HiddenFor(rs => rs.Assets);
<div>
<div class="editor-label">Barcode</div>
<div class="editor-field"><input type="text" name="Barcode" value="@Model.Barcode" /></div>
</div>
<div>
<div class="editor-label">RFID</div>
<div class="editor-field"><input type="text" name="Barcode" value="@Model.RFID" /></div>
</div><br />
..。
@for (int i = 0; i < Model.Assets.Count; i++)
{
<tr>
<td>@Html.DisplayFor(asm => asm.Assets[i].Asset.InventoryNumber)</td>
<td>@Html.DisplayFor(asm => asm.Assets[i].Asset.Description)</td>
<td>@Html.DisplayFor(asm => asm.Assets[i].Asset.Manufacturer)</td>
<td>@Html.DisplayFor(asm => asm.Assets[i].Asset.Model)</td>
<td>@Html.DisplayFor(asm => asm.Assets[i].Asset.SerialNumber)</td>
<td>@Html.DisplayFor(asm => asm.Assets[i].Scanned)</td>
<td>@Html.DisplayFor(asm => asm.Assets[i].CheckIn)</td>
</tr>
}
「Asset[i]」を追加したのは、デフォルトのモデルバインダーが適切にバインドするのに役立つ場所を読んだためです(動作しませんでした)
私の問題は:私のコントローラーで:
[HttpPost]
public ActionResult Scan(RoomScan toVerify)
リストは空です(nullではありません)。これがモデルバインダーに関係していることは知っていますが、機能するように変更する方法がわかりません。