2014-02-28 77 views
1

我知道這個問題之前已經被問過了,但是我已經按照這樣的其他線索回答了這個問題,但仍然無法解決我的問題。在物品列表中發佈一個列表 - 無法檢索控制器中的物品

所以我有一個列表中有另一個列表的形式,我想在我的後動作控制器中檢索所有這些項目,所以我可以更新我的控制器中的權限。但我目前無法在我的帖子操作中檢索我的項目,我不知道爲什麼。

查看:

@using (Html.BeginForm()) 
       { 
        <ul class="nav well-tabs well-tabs-inverse margin-bottom"> 
         @for (int counter = 0; Model.Count > counter; counter++) 
         { 
          <li @if (counter == 0) { @Html.Raw("class='active'") }><a href="@Html.Raw("#tab" + counter)" data-toggle="tab">@Model[counter].Permissions.RoleName</a></li> 
         } 
        </ul> 
        <!-- Rechten Content --> 
        <div class="tab-content border-none padding-none"> 

         @for (int counter = 0; Model.Count > counter; counter++) 
         { 
          <div @Html.Raw(string.Format("id=tab{0}", counter)) class="tab-pane @if (counter == 0) 
                           { 
                            @Html.Raw("active") 
                           }"> 
           <table class="table table-widget table-striped"> 
            <thead> 
             <tr> 
              <th>Recht</th> 
              <th>Machtiging</th> 
             </tr> 
            </thead> 
            <tbody> 
             <!-- Get all the updated permissions --> 
             @for (int counter2 = 0; Model[counter].Permissions.Rights.Count() > counter2; counter2++) 
             { 
              <tr> 
               <td>@Model[counter].Permissions.Rights[counter2].RightName</td> 
               <td>@Html.CheckBoxFor(m=> m[counter].Items[counter2].Selected) 
               @Html.HiddenFor(m => m[counter].Items[counter2].RightId) 
               @Html.HiddenFor(m => m[counter].Items[counter2].RoleName) 
               </td> 
              </tr> 
             } 
            </tbody> 
           </table> 
          </div> 
         } 
         <input type="submit" value="Gegevens opslaan" css="btn btn-info" /> 
        </div> 

       } 

型號:

public class ViewRolePermissionsModel 
{ 
    public Permissions Permissions { get; set; } 
    public List<Items> Items; 
} 

// Sub class 
public class Permissions 
{ 
    public string RoleName { get; set; } 
    public List<RightModel> Rights { get; set; } 
} 

// Sub class 
public class RightModel 
{ 
    public int RightId { get; set; } 
    public string RightName { get; set; } 
} 

// Sub class 
public class Items 
{ 
    public int RightId { get; set; } 
    public string RoleName { get; set; } 
    public bool Selected { get; set; } 
} 

這導致,因爲這在我看來: End result html

這是我的控制器產生什麼我也想獲得在我發佈的操作中: Controller generated code

什麼我的帖子行動檢索: enter image description here

後的結果提交,就在項目(這就是我需要)

[0].Items[0].RightId 1 
[0].Items[0].RoleName Administratoren 
[0].Items[0].Selected false 
[0].Items[10].RightId 11 
[0].Items[10].RoleName Administratoren 
[0].Items[10].Selected false 
[0].Items[11].RightId 12 
[0].Items[11].RoleName Administratoren 
[0].Items[11].Selected false 
[0].Items[1].RightId 2 
[0].Items[1].RoleName Administratoren 
[0].Items[1].Selected false 
[0].Items[2].RightId 3 
[0].Items[2].RoleName Administratoren 
[0].Items[2].Selected false 
[0].Items[3].RightId 4 
[0].Items[3].RoleName Administratoren 
[0].Items[3].Selected false 
[0].Items[4].RightId 5 
[0].Items[4].RoleName Administratoren 
[0].Items[4].Selected false 
[0].Items[5].RightId 6 
[0].Items[5].RoleName Administratoren 
[0].Items[5].Selected false 
[0].Items[6].RightId 7 
[0].Items[6].RoleName Administratoren 
[0].Items[6].Selected false 
[0].Items[7].RightId 8 
[0].Items[7].RoleName Administratoren 
[0].Items[7].Selected false 
[0].Items[8].RightId 9 
[0].Items[8].RoleName Administratoren 
[0].Items[8].Selected false 
[0].Items[9].RightId 10 
[0].Items[9].RoleName Administratoren 
[0].Items[9].Selected false 
[1].Items[0].RightId 1 
[1].Items[0].RoleName Leden 
[1].Items[0].Selected false 
[1].Items[10].RightId 11 
[1].Items[10].RoleName Leden 
[1].Items[10].Selected false 
[1].Items[11].RightId 12 
[1].Items[11].RoleName Leden 
[1].Items[11].Selected false 
[1].Items[1].RightId 2 
[1].Items[1].RoleName Leden 
[1].Items[1].Selected false 
[1].Items[2].RightId 3 
[1].Items[2].RoleName Leden 
[1].Items[2].Selected false 
[1].Items[3].RightId 4 
[1].Items[3].RoleName Leden 
[1].Items[3].Selected false 
[1].Items[4].RightId 5 
[1].Items[4].RoleName Leden 
[1].Items[4].Selected false 
[1].Items[5].RightId 6 
[1].Items[5].RoleName Leden 
[1].Items[5].Selected false 
[1].Items[6].RightId 7 
[1].Items[6].RoleName Leden 
[1].Items[6].Selected false 
[1].Items[7].RightId 8 
[1].Items[7].RoleName Leden 
[1].Items[7].Selected false 
[1].Items[8].RightId 9 
[1].Items[8].RoleName Leden 
[1].Items[8].Selected false 
[1].Items[9].RightId 10 
[1].Items[9].RoleName Leden 
[1].Items[9].Selected false 
[2].Items[0].RightId 1 
[2].Items[0].RoleName Medewerkers 
[2].Items[0].Selected false 
[2].Items[10].RightId 11 
[2].Items[10].RoleName Medewerkers 
[2].Items[10].Selected false 
[2].Items[11].RightId 12 
[2].Items[11].RoleName Medewerkers 
[2].Items[11].Selected false 
[2].Items[1].RightId 2 
[2].Items[1].RoleName Medewerkers 
[2].Items[1].Selected false 
[2].Items[2].RightId 3 
[2].Items[2].RoleName Medewerkers 
[2].Items[2].Selected false 
[2].Items[3].RightId 4 
[2].Items[3].RoleName Medewerkers 
[2].Items[3].Selected false 
[2].Items[4].RightId 5 
[2].Items[4].RoleName Medewerkers 
[2].Items[4].Selected false 
[2].Items[5].RightId 6 
[2].Items[5].RoleName Medewerkers 
[2].Items[5].Selected false 
[2].Items[6].RightId 7 
[2].Items[6].RoleName Medewerkers 
[2].Items[6].Selected false 
[2].Items[7].RightId 8 
[2].Items[7].RoleName Medewerkers 
[2].Items[7].Selected false 
[2].Items[8].RightId 9 
[2].Items[8].RoleName Medewerkers 
[2].Items[8].Selected false 
[2].Items[9].RightId 10 
[2].Items[9].RoleName Medewerkers 
[2].Items[9].Selected false 
+0

什麼是您的實際問題呢?目前還不清楚。 – nik0lias

+0

對不起,我的問題是:我如何檢索我的發佈行爲中的項目列表?因爲我目前無法檢索我的物品。 – Jamie

+0

你可以發佈更多的DOM嗎?我只能看到第一個例子。對於模型中的每個對象,它應該生成一個新的索引。如果不是,你需要手動構建它。例如,我只能看到[0] .Items [0] .Selected。下一個應該是[0] .Items [1] .Selected ... etc – nik0lias

回答

1

拼湊起來的一些示例代碼。首先,控制方法:

public ActionResult ProcessModel() 
{ 
    ViewRolePermissionsModel newResponse = ProcessRoles(Request.Form); 
} 

,然後方法解析到模型

private ViewRolePermissionsModel ProcessRoles(System.Collections.Specialized.NameValueCollection requestForm) 
{ 
    ViewRolePermissionsModel viewRolePermissionsModel = new ViewRolePermissionsModel(); 

    String[] allKeys = requestForm.AllKeys; 

    foreach (String localKey in allKeys) 
    { 
     itemValue = requestForm.GetValues(localKey).FirstOrDefault(); 
     // depending on the localKey either use this to add the value to the model or to build a list to be added later 
    } 
} 
+0

工程作爲一種魅力,但真的應該是一個合法的方式來做到這一點。 – Jamie