视图模型&GT;如何在处理一个List&LT使用@ Html.CheckBoxFor(); [英] How to use @Html.CheckBoxFor() while dealing with a List<ViewModel>
问题描述
这是我的看法。如何使用CheckboxFor():
@using eMCViewModels;
@model eMCViewModels.RolesViewModel
@ {
ViewBag.Title =CreateNew;
}
&LT; H2&GT;
CreateNew&LT;
/ H2&GT;@using(Html.BeginForm())
{
@ Html.ValidationSummary(真) &LT;&字段集GT;
&LT;传奇&GT; RolesViewModel&LT; /传说&GT;
&LT; DIV CLASS =编辑标记&GT;
@ Html.LabelFor(型号=&GT; model.Name)
&LT; / DIV&GT;
&LT; DIV CLASS =主编场&GT;
@ Html.EditorFor(型号=&GT; model.Name)
@ Html.ValidationMessageFor(型号=&GT; model.Name)
&LT; / DIV&GT;
&LT; DIV&GT;
@foreach(RoleAccessViewModel MNU在Model.RoleAccess)
{
//如何在这里使用checkboxfor?
}
&LT; / DIV&GT;
&所述p为H.;
&LT;输入类型=提交值=创建/&GT;
&所述; / P&GT;
&LT; /字段集&GT;
}
&LT; DIV&GT;
@ Html.ActionLink(返回目录,索引)
&LT; / DIV&GT;
@section脚本{
@ Scripts.Render(〜/包/ jqueryval)
}
Model.RoleAccess
是列表&LT; RoleAccessViewModel&GT;
和我想用创建复选框 @ Html.CheckBoxFor()
。
这是我的 RoleAccessViewModel
公共类RoleAccessViewModel
{
公众诠释角色ID {搞定;组; }
公共字符串角色名{获得;组; }
公众诠释MENUID {搞定;组; }
公共字符串MenuDisplayName {搞定;组; }
公共字符串MenuDiscription {搞定;组; }
公共字符串IsEnabled {搞定;组; } //改变为bool
}
假设我有列表中的5个项目,那么我需要5个复选框与ID = MENUID和Text = menuDisplayName。我怎样才能做到这一点?
修改
我尝试
<$p$p><$c$c>@Html.CheckBoxFor(x=>x.RoleAccess.SingleOrDefault(r=>r.MenuID==mnu.MenuID).IsEnabled )@ Html.LabelFor(X =&GT; x.RoleAccess.SingleOrDefault(R = GT; r.MenuID == mnu.MenuID).MenuDisplayName)
但是,从字符串
更改 IsEnabled
的类型布尔$ C后$ C>。复选框工作。但标签打印仅
MenuDisplayName
而不是值。任何一个可以帮助?
更改的foreach与
@for(INT I = 0; I&LT; Model.RoleAccess.Count;我++)
{
Html.CheckBoxFor(Model.RoleAccess [I] .IsEnabled,新{ID = Model.RoleAccess [I] .MenuID});
Html.DisplayFor(Model.RoleAccess [I] .MenuDisplayName); //或者只是Model.RoleAccess [I] .MenuDisplayName
}
This is my View. How to use 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
is a List<RoleAccessViewModel>
and I want to create checkbox using @Html.CheckBoxFor()
.
This is my 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
}
Suppose I have 5 items in list, then I need 5 checkbox with ID=menuID and Text = menuDisplayName. How can I achieve this?
EDIT
My attempt
@Html.CheckBoxFor(x=>x.RoleAccess.SingleOrDefault(r=>r.MenuID==mnu.MenuID).IsEnabled )
@Html.LabelFor(x=>x.RoleAccess.SingleOrDefault(r=>r.MenuID==mnu.MenuID ).MenuDisplayName )
But after changing the type of IsEnabled
from string
to bool
. The checkbox works. But Label prints only MenuDisplayName
instead of values . Can any one helps ?
Change foreach with for
@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
}
这篇关于视图模型&GT;如何在处理一个List&LT使用@ Html.CheckBoxFor();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!