这是我的观点。如何使用 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 和 Text = menuDisplayName 的复选框。我怎样才能做到这一点?
编辑
我的尝试
@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
from的类型更改string
为之后bool
。复选框有效。但是 Label 只打印MenuDisplayName
而不是 values 。任何人都可以帮忙吗?