这是我的观点。如何使用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
的类型从string
更改为bool
。该复选框有效。但Label仅打印MenuDisplayName
而不是值。任何人都可以帮忙吗?
答案 0 :(得分:14)
如果我理解正确,你的意思是这个?并且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 )
}
答案 1 :(得分:9)
使用for
更改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
}
答案 2 :(得分:8)
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)
答案 3 :(得分:0)
我知道它已经很老但是
在您的模型中使用DataAnotations Display Attribute
public class MyModel
int ID{get; set;};
[Display(Name = "Last Name")]
string LastName {get; set;};
end class