2013-02-12 130 views
2

我遇到了模型绑定的问题。 来到这里以下型号: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) 

列表为空(非空)。 我知道这与模型联编程序有关,但我不熟悉如何更改它以使其工作。

+0

首先更改DisplayFor到EditorFor ......再看看生成的html。接下来确保输入名称看起来像是验证.Assests [0] .Assest.Inventory等。 – 2013-02-12 10:15:23

+0

你应该使用'public ActionResult Scan(IEnumerable toVerify){}'。请检查[本](http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx) – 2013-02-12 10:46:52

+0

@DavidPerlman我只是显示列表,应该没有编辑字段。整个列表是表单的一部分,您可以将RFID或条形码发送给控制器。要检查扫描资产并设置“已扫描”= true 此外,RoomScan型号不包含List,它只包含一个。 – Samsa 2013-02-12 11:26:17

回答

1

我一直在使用这个绑定复杂模型与嵌套列表。我几乎总是与这个权利了蝙蝠替换默认的模型绑定...

A DefaultModelBinder that can update complex model graphs

我希望这可以帮助你的,因为它没有我很多。

+0

看起来像正确的方向。 我实现了它,但在控制器中AssetScanModel列表为空。我检查了新的modelBinder,他甚至没有从视图中获取AssetScanModel列表。多数民众赞成在奇怪的,我认为有一个大错误,我编码在 – Samsa 2013-02-12 12:59:16

+0

在我的身边在视图中发现了一些错误(似乎HTML帮手往往被忽略,如果他们不包裹在一个HTML标签(div等) – Samsa 2013-02-13 08:03:04

+0

啊,是的那会得到你很高兴你能弄清楚它。 – 2013-02-13 10:44:37

0

尝试DisplayTemplates/EditorTemplates,为您的嵌套模型类型创建模板。 在View写:

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

在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>