我遇到了模型綁定的問題。 來到這裏以下型號:c#mvc模型綁定嵌套列表
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>
}
我添加了 「資產[I]」 因爲我讀在某處它可以幫助默認的模型綁定器正確綁定(didn#t work)
我的問題是:在我的c ontroller:
[HttpPost]
public ActionResult Scan(RoomScan toVerify)
列表爲空(非空)。 我知道這與模型聯編程序有關,但我不熟悉如何更改它以使其工作。
首先更改DisplayFor到EditorFor ......再看看生成的html。接下來確保輸入名稱看起來像是驗證.Assests [0] .Assest.Inventory等。 – 2013-02-12 10:15:23
你應該使用'public ActionResult Scan(IEnumerable toVerify){}'。請檢查[本](http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx) –
2013-02-12 10:46:52
@DavidPerlman我只是顯示列表,應該沒有編輯字段。整個列表是表單的一部分,您可以將RFID或條形碼發送給控制器。要檢查掃描資產並設置「已掃描」= true 此外,RoomScan型號不包含List,它只包含一個。 – Samsa 2013-02-12 11:26:17