如何在处理列表<ViewModel>时使用 @Html.CheckBoxFor()



这是我的观点。如何使用复选框((:

@using eMCViewModels;
@model eMCViewModels.RolesViewModel
@{
    ViewBag.Title = "CreateNew";
}
<h2>
    CreateNew<
/h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>RolesViewModel</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div>
            @foreach (RoleAccessViewModel mnu in Model.RoleAccess)
            {
                 // How to use checkboxfor here?
            }
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Model.RoleAccess是一个List<RoleAccessViewModel>,我想使用 @Html.CheckBoxFor() 创建复选框。

这是我的RoleAccessViewModel

public class RoleAccessViewModel
{
    public int RoleID { get; set; }
    public string RoleName { get; set; }
    public int MenuID { get; set; }
    public string MenuDisplayName { get; set; }
    public string MenuDiscription { get; set; }
    public string IsEnabled { get; set; } // changed to bool
}

假设我在列表中有 5 个项目,那么我需要 5 个带有 ID = menuID 和文本 = 菜单显示名称的复选框。我怎样才能做到这一点?

编辑

我的尝试

@Html.CheckBoxFor(x=>x.RoleAccess.SingleOrDefault(r=>r.MenuID==mnu.MenuID).IsEnabled )
@Html.LabelFor(x=>x.RoleAccess.SingleOrDefault(r=>r.MenuID==mnu.MenuID ).MenuDisplayName ) 

但是在将IsEnabled类型从string更改为bool之后.该复选框有效。但是标签只打印MenuDisplayName而不是值。任何人都可以帮忙吗?

如果我不对,你的意思是这个?IsEnabled应该布尔类型

public class RoleAccessViewModel
{
    public int RoleID { get; set; }
    public string RoleName { get; set; }
    public int MenuID { get; set; }
    public string MenuDisplayName { get; set; }
    public string MenuDiscription { get; set; }
    public bool IsEnabled { get; set; }
}

视图

@foreach (RoleAccessViewModel mnu in Model.RoleAccess)
{
    @Html.CheckBoxFor(m => mnu.IsEnabled )
}

将 foreach 更改为

@for(int i = 0; i < Model.RoleAccess.Count; i++)
{
    Html.CheckBoxFor(Model.RoleAccess[i].IsEnabled, new { id = Model.RoleAccess[i].MenuID });
    Html.DisplayFor(Model.RoleAccess[i].MenuDisplayName); // or just Model.RoleAccess[i].MenuDisplayName
}

CheckBoxFor仅适用于布尔属性。因此,您需要做的第一件事是修改视图模型,以便包含一个布尔属性,指示是否选择了记录:

public class RoleAccessViewModel
{
    public int RoleID { get; set; }
    public string RoleName { get; set; }
    public int MenuID { get; set; }
    public string MenuDisplayName { get; set; }
    public string MenuDiscription { get; set; }
    public bool IsEnabled { get; set; }
}

然后我建议用编辑器模板替换您的foreach循环:

<div>
    @Html.EditorFor(x => x.RoleAccess)
</div>

最后编写相应的编辑器模板,该模板将自动呈现给 RolesAccess 集合的每个元素(~/Views/Shared/EditorTemplates/RoleAccessViewModel.cshtml(:

@model RoleAccessViewModel
@Html.HiddenFor(x => x.RoleID)
... might want to include additional hidden fields
... for the other properties that you want to be bound back
@Html.LabelFor(x => x.IsEnabled, Model.RoleName)
@Html.CheckBoxFor(x => x.IsEnabled)
我知道

它很老了,但是

在模型中使用数据注释显示属性

public class MyModel
   int ID{get; set;};
   [Display(Name = "Last Name")]
   string LastName {get; set;};
end class

最新更新