强类型视图在ViewModel中发送null集合

本文关键字:null 集合 视图 ViewModel 强类型 | 更新日期: 2023-09-27 18:07:08

 public class UserSettingsViewModel
{
    [Display(Name="Kullanıcılar")]
    public List<UserFormViewModel> Users { get; set; }
    [Display(Name = "Roller")]
    public IEnumerable<SYS_ROLE> SYS_ROLE { get; set; }
    public RoleEditViewModel CurrentRoleEdit { get; set; }
}
public class RoleEditViewModel
{
    public int RoleID { get; set; }
    [Display(Name="Rol"),Required(ErrorMessage="Rol boş geçilemez.")]
    public string RoleName { get; set; }
    public List<RoleRightModel> RoleRights { get; set; }
}
public class RoleRightModel
{
    public int ID { get; set; }
    [Display(Name = "Tanım")]
    public string NAME_ { get; set; }
    [Display(Name = "Açıklama")]
    public string DESC_ { get; set; }
    public bool CHECKED { get; set; }
}

In View谁使用UserSettingViewModel

    @model PlusNet.Models.ViewModels.UserSettingsViewModel
//some code here
 @Html.Partial("_RoleEdit", Model.CurrentRoleEdit)

部分视图正确呈现。下面是_RoleEdit部分视图源代码

@ model PlusNet.Models.ViewModels.RoleEditViewModel的方式@using (Html。BeginForm("RoleEdit","设置")){@Html。HiddenFor(model => model. roleid)

            <div class="form-group mt-lg">
                <label class="col-sm-3 control-label">@Html.DisplayNameFor(model => model.RoleName)</label>
                <div class="col-sm-9">
                    @*<input type="text" id="txt_role" class="form-control" />*@
                    @Html.TextBoxFor(model => model.RoleName, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.RoleName)
                </div>
            </div>
            <table class="table table-bordered table-striped mb-none deftable-nav" id="rightsgrid">
                <thead>
                    <tr>
                        <th>@Html.DisplayNameFor(model => model.RoleRights.FirstOrDefault().NAME_)</th>
                        <th>@Html.DisplayNameFor(model => model.RoleRights.FirstOrDefault().DESC_)</th>
                        <th class="center no-sort">İşlem</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model.RoleRights)
                    {
                        <tr class="tablerow" data-id="@item.ID">
                            <td>@item.NAME_</td>
                            <td>@item.DESC_</td>
                            <td class="center">
                                <div class="checkboxdiv">
                                    <div class="checkbox-custom checkbox-primary">
                                        @*<input type="checkbox" checked="" class="chck_right">*@
                                        @Html.CheckBox("CHECKED")
                                        <label for="chck_right"></label>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

            <footer class="panel-footer">
                <div class="row">
                    <div class="col-md-12 text-right">
                        <input type="hidden" id="hidden_roleid" value="0" />
                        <button class="btn btn-primary">Kaydet</button>
                        <button class="btn btn-default modal-dismiss" id="btn_modalrole_cancel">İptal</button>
                    </div>
                </div>
            </footer>
        }
    </div>
</section>

生成得很好,不是空的

@foreach (var item in Model.RoleRights)

当表单提交和控制器动作触发时

public ActionResult RoleEdit(RoleEditViewModel vm)

虚拟机对象已满。除了RoleRights收藏。它总是空的。怎么了?我已经尝试过editortemplate,没有html。复选框,html。行动,viewdatadictionary……总之,集合在post方法中为空。其他视图模型属性已满。

强类型视图在ViewModel中发送null集合

我认为你必须使用for循环而不是foreach因为for循环创建复选框控件名称与索引

@for (int i = 0; i < Model.RoleRights.Count; i++)
{
     @Html.CheckBoxFor(x => Model.RoleRights[i].CHECKED)
} 

我用editortemplates修复了它。这个例子帮助了我。http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add.html

您写CHECKED输入的方式不正确。通过这种方式,表单向服务器发送一个请求参数,命名为:CHECKED要将它正确地绑定到您的RoleEditViewModel,它需要是:RoleRights[index].CHECKED其中index是一个从0开始的数字序列。所以,如果你要发送3个滚动权限,请求参数其中:

RoleRights[0].CHECKED
RoleRights[1].CHECKED
RoleRights[2].CHECKED

在razor中,foreach循环看起来像这样:

@{int index = 0;}
@foreach (var item in Model.RoleRights)
{
    @Html.CheckBox(string.format("RoleRights[{0}].CHECKED", ++index), item.CHECKED)
}

请参考Phil Haacks关于列表模型绑定的文章,以及一些更复杂的例子http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/