简体   繁体   English

ViewModel中的强类型视图发布空集合

[英]Strongly Typed View Posting null collection in ViewModel

 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 who using UserSettingViewModel 在View中使用UserSettingViewModel的用户

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

partial view rendering properly. 局部视图正确渲染。 Here is the _RoleEdit partial view source 这是_RoleEdit部分视图源

@model PlusNet.Models.ViewModels.RoleEditViewModel Rol @using (Html.BeginForm("RoleEdit", "Settings")) { @Html.HiddenFor(model => model.RoleID) @model PlusNet.Models.ViewModels.RoleEditViewModel Rol @using(Html.BeginForm(“ RoleEdit”,“ Settings”)){@ 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>

` `

its generated very well and its not empty 它生成的很好,并且不是空的

@foreach (var item in Model.RoleRights) @foreach(Model.RoleRights中的var项目)

when form submited and controller action fired 提交表单并触发管制员行动时

public ActionResult RoleEdit(RoleEditViewModel vm) 公共ActionResult RoleEdit(RoleEditViewModel vm)

vm object is full. vm对象已满。 except RoleRights collection. 除了RoleRights集合。 Its comming always null. 它来的总是空的。 What is wrong? 怎么了? I already tried editortemplate, without html.checkbox, html.action, viewdatadictionary... Anyway collection coming null to the post method. 我已经尝试过editor模板,没有html.checkbox,html.action,viewdatadictionary ...反正post方法的集合为null。 Other viewmodel property is full. 其他viewmodel属性已满。

I think you have to use for loop instead of foreach because for loop create checkbox control name with index. 我认为您必须使用for循环而不是foreach,因为for循环使用索引创建复选框控件名称。

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

I fixed it with editortemplates. 我用editortemplates修复了它。 This example helped me. 这个例子对我有帮助。 http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add.html http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add.html

The way you write the CHECKED input is not correct. 您编写CHECKED输入的方式不正确。 This way the form sends a request parameter to the server which is just named: CHECKED To have it bound correctly to your RoleEditViewModel it would need to be: RoleRights[index].CHECKED where index is a numeric sequence starting with 0. So, if you would send 3 rolerights, the rquest parameter where: 这样,表单将请求参数发送到仅命名为服务器的服务器: CHECKED若要将其正确绑定到RoleEditViewModel,它将需要为: RoleRights[index].CHECKED ,其中index是从0开始的数字序列。因此,如果您将发送3个角色,rquest参数,其中:

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

In razor your foreach loop would look like this: 在razor中,您的foreach循环如下所示:

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

Please refer to Phil Haacks article about model binding of lists for this and some more complicated examples http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ 有关此列表以及一些更复杂的示例,请参阅Phil Haacks文章中有关列表的模型绑定的信息, 网址为http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM