gpt4 book ai didi

c# mvc 模型绑定(bind)嵌套列表

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:23 26 4
gpt4 key购买 nike

我的模型绑定(bind)有问题。获得以下型号:

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; }
}

View 确实列出了所有 Assets :

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]”,因为我在某处读到它有助于默认模型绑定(bind)器正确绑定(bind)(没有#t 工作)

我的问题是:在我的 Controller 中:

[HttpPost]
public ActionResult Scan(RoomScan toVerify)

列表为空(不为空)。我知道这与模型绑定(bind)器有关,但我不熟悉如何更改它以使其正常工作。

最佳答案

我一直在用它来绑定(bind)带有嵌套列表的复杂模型。我几乎总是立即用这个替换默认模型 Binder ......

A DefaultModelBinder that can update complex model graphs

我希望这对你和我一样有帮助。

关于c# mvc 模型绑定(bind)嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com