简体   繁体   English

C#MVC模型绑定嵌套列表

[英]c# mvc model binding nested lists

I got a problem with my model binding. 我的模型绑定有问题。 Got here following models: 在这里获得以下型号:

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

The View does list all the 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>
}

I added the "Asset[i]" because I read somewhere it helps the default model binder to bind propperly (didn#t work) 我添加了“ Asset [i]”,因为我在某处读到它有助于默认模型绑定器正确绑定(didn#t工作)

My problem is: in my controller: 我的问题是:在我的控制器中:

[HttpPost]
    public ActionResult Scan(RoomScan toVerify)

the List is empty (not null). 列表为空(不为null)。 I know this has to do with the model binder, but I am not familiar how to change it so it does work. 我知道这与模型联编程序有关,但是我不熟悉如何对其进行更改以使其起作用。

I have been using this for binding complex models with nested lists. 我一直在用它来绑定带有嵌套列表的复杂模型。 I almost always replace the default model binder with this right off the bat... 我几乎总是立即用这个替换默认的模型装订器。

A DefaultModelBinder that can update complex model graphs 一个可以更新复杂模型图的DefaultModelBinder

I hope this helps you as much as it did me. 希望这对您有所帮助。

Try DisplayTemplates/EditorTemplates, create template for ur nested model type. 尝试使用DisplayTemplates / EditorTemplates,为您的嵌套模型类型创建模板。 In View write: 在视图中写道:

@Html.DisplayFor(asm => asm.Assets)

in DisplayTemplate (AssetScanModel.cshtml): 在DisplayTemplate(AssetScanModel.cshtml)中:

@model AssetScanModel

<tr>
    <td>@Html.DisplayFor(asm => asm.Asset.InventoryNumber)</td>
    <td>@Html.DisplayFor(asm => asm.Asset.Description)</td>
    <td>@Html.DisplayFor(asm => asm.Asset.Manufacturer)</td>
    <td>@Html.DisplayFor(asm => asm.Asset.Model)</td>
    <td>@Html.DisplayFor(asm => asm.Asset.SerialNumber)</td>
    <td>@Html.DisplayFor(asm => asm.Scanned)</td>
    <td>@Html.DisplayFor(asm => asm.CheckIn)</td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM