在项目列表中发布列表 - 无法检索控制器中的项目

时间:2014-02-28 08:54:16

标签: asp.net-mvc model-binding

我知道之前已经问过这个问题,但是我已经按照这样的其他线程的答案,但仍然无法解决我的问题。

所以我所拥有的是一个包含列表的表单,其中包含另一个列表,我想在我的帖子操作控制器中检索所有这些项目,以便我可以更新控制器中的权限。但是我目前无法在我的帖子中检索我的项目而且我不知道为什么。

查看:

@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

1 个答案:

答案 0 :(得分: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
    }
}