2013-02-12 202 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>