2013-04-23 46 views
3

我的模型:MVC回報人口從視圖模型的形式提交後

public class SendFileDeviceViewModel 
    { 
    public SendFileDeviceViewModel() 
    { 
     PolicyList = new List<SendFileDevicePoliciesViewModel>(); 
    } 
    public string DeviceName { get; set; } 
    public int DeviceId { get; set; } 
    public string ManagementGroupName { get; set; } 
    public int ManagementGroupId { get; set; } 
    public bool ReloadConfiguration { get; set; } 
    public bool ImmediateSend { get; set; } 
    public DateTime TimeToSend { get; set; } 
    public List<SendFileDevicePoliciesViewModel> PolicyList { get; set; } 
    } 
    public class SendFileDevicePoliciesViewModel 
    { 
    public int PackageTemplateId { get; set; } 
    public string PolicyName { get; set; } 
    public string PolicyType { get; set; } 
    public string DefinedAt { get; set; } 
    public bool ResendPolicy { get; set; } 
    } 

筆者認爲:

<h2>Send files to a Device @Model.DeviceName</h2> 
    <h3>Reload configuration settings</h3> 
    @Html.CheckBoxFor(m => m.ReloadConfiguration) @Html.LabelFor(m => m.ReloadConfiguration) 
    <h3>Select the policies to reload</h3> 
    @using (Html.BeginForm()) 
    { 

    @Html.HiddenFor(m => m.DeviceId) 
    @Html.HiddenFor(m => m.ManagementGroupId) 

    @Html.ValidationSummary(true) 

    if (Model.PolicyList.Count() > 0) 
    { 
    <table> 
     <caption> 
     Policies available for this device</caption> 
     <thead> 
     <tr> 
      <th scope="col"> 
      &nbsp; 
      </th> 
      <th scope="col"> 
      Policy Name 
      </th> 
      <th scope="col"> 
      Policy Type 
      </th> 
      <th scope="col"> 
      Defined At 
      </th> 
     </tr> 
     </thead> 
     <tbody> 
     @foreach (var policies in Model.PolicyList) 
     { 
      <tr> 
      @*<td>@Html.CheckBox("PackageTemplateId", new { value = policies.PackageTemplateId })</td>*@ 
      <td>@Html.CheckBoxFor(m => policies.ResendPolicy)</td> 
      <td>@policies.PolicyName</td> 
      <td>@policies.PolicyType</td> 
      <td>@policies.DefinedAt</td> 
      </tr> 
     } 
     </tbody> 
    </table> 
    } 

    <div class="editor-label"> 
     @Html.LabelFor(m => m.ImmediateSend) 
    </div> 
    <div class="editor-field"> 
     @Html.CheckBoxFor(m => m.ImmediateSend) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(m => m.TimeToSend) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(m => m.TimeToSend) 
    </div> 
    <p> 
     <input type="submit" value="Send files" /></p> 

我的問題是,從獲取控制器模型時PolicyList總是空的。我在這裏錯過簡單的東西嗎?

+0

哪裏控制器代碼來填充'PolicyList'? – 2013-04-23 10:44:34

+0

在我的控制器:)我沒有包括它,因爲視圖填充很好,它的回報我關心。如果它的相關我可以發佈,但我不知道它是如何。 – DavidB 2013-04-23 10:53:45

回答

5

兩個問題:

你的第一個問題是,你在你的構造函數重置您的列表,所以當窗體已發佈並且模型聯編程序實例化模型的一個實例,您將重新設置該列表。將其更改爲做一個聚結,只有重新分配,如果該列表是null

public SendFileDeviceViewModel() 
{ 
    PolicyList = PolicyList ?? new List<SendFileDevicePoliciesViewModel>(); 
} 

你的下一個問題是你foreach。爲了正確索引name屬性(因此模型聯編程序可以做到這一點),您需要使用for循環。另外,請將ID保持在HiddenFor

到位您foreach的試試這個:

@for (int i = 0; i < Model.PolicyList.Count; i++) 
{ 
    <tr> 
     <td> 
      @Html.HiddenFor(m => m.PolicyList[i].PackageTemplateId) 
      @Html.CheckBoxFor(m => m.PolicyList[i].ResendPolicy) 
     </td> 
     <td>@Model.PolicyList[i].PolicyName</td> 
     <td>@Model.PolicyList[i].PolicyType</td> 
     <td>@Model.PolicyList[i].DefinedAt</td> 
    </tr> 
} 
+0

輝煌,謝謝你matty – DavidB 2013-04-23 10:56:09

+0

@DavidB沒問題,很高興我可以幫助:) – mattytommo 2013-04-23 10:58:58

+0

不幸的是我的名單仍然是空的,有什麼想法? – DavidB 2013-04-23 11:01:28

3

原因是因爲您沒有尊重您輸入字段的naming convention

<tbody> 
    @for (var i = 0; i < Model.PolicyList.Count; i++) 
    { 
     <tr> 
      <td>@Html.CheckBoxFor(x => x.PolicyList[i].ResendPolicy)</td> 
      <td>@Html.DisplayFor(x => x.PolicyList[i].PolicyName)</td> 
      <td>@Html.DisplayFor(x => x.PolicyList[i].PolicyType)</td> 
      <td>@Html.DisplayFor(x => x.PolicyList[i].DefinedAt)</td> 
     </tr> 
    } 
</tbody> 

而且現在只有ResendPolicy屬性將被約束,因爲這是有一個相應的輸入字段(複選框只有一個:你應該用for循環或自定義編輯模板替換鑑於foreach環你的情況)。如果你要綁定的其他人也可能需要包括相應的隱藏字段:

<tbody> 
    @for (var i = 0; i < Model.PolicyList.Count; i++) 
    { 
     <tr> 
      <td> 
       @Html.HiddenFor(x => x.PolicyList[i].PackageTemplateId) 
       @Html.CheckBoxFor(x => x.PolicyList[i].ResendPolicy) 
      </td> 
      <td> 
       @Html.HiddenFor(x => x.PolicyList[i].PolicyName) 
       @Html.DisplayFor(x => x.PolicyList[i].PolicyName) 
      </td> 
      <td> 
       @Html.HiddenFor(x => x.PolicyList[i].PolicyType) 
       @Html.DisplayFor(x => x.PolicyList[i].PolicyType) 
      </td> 
      <td> 
       @Html.HiddenFor(x => x.PolicyList[i].DefinedAt) 
       @Html.DisplayFor(x => x.PolicyList[i].DefinedAt) 
      </td> 
     </tr> 
    } 
</tbody> 
+0

ahh謝謝你的幫助 – DavidB 2013-04-23 10:55:37

+0

不客氣。 – 2013-04-23 10:56:09