视图模型>如何在处理一个List&LT使用@ Html.CheckBoxFor(); [英] How to use @Html.CheckBoxFor() while dealing with a List<ViewModel>

查看:149
本文介绍了视图模型>如何在处理一个List&LT使用@ Html.CheckBoxFor();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的看法。如何使用CheckboxFor():

  @using eMCViewModels;
@model eMCViewModels.RolesViewModel
@ {
    ViewBag.Title =CreateNew;
}
< H2>
    CreateNew<
/ H2>@using(Html.BeginForm())
{
    @ Html.ValidationSummary(真)    <&字段集GT;
        <传奇> RolesViewModel< /传说>
        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=> model.Name)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.EditorFor(型号=> model.Name)
            @ Html.ValidationMessageFor(型号=> model.Name)
        < / DIV>
        < D​​IV>
            @foreach(RoleAccessViewModel MNU在Model.RoleAccess)
            {
                 //如何在这里使用checkboxfor?
            }
        < / DIV>
        &所述p为H.;
            <输入类型=提交值=创建/>
        &所述; / P>
    < /字段集>
}
< D​​IV>
    @ Html.ActionLink(返回目录,索引)
< / DIV>
@section脚本{
    @ Scripts.Render(〜/包/ jqueryval)
}

Model.RoleAccess 列表< RoleAccessViewModel> 和我想用创建复选框 @ 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 的类型布尔。复选框工作。但标签打印仅 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆