C# 如何在处理 List<ViewModel> 时使用 @Html.CheckBoxFor()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11652606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:40:45  来源:igfitidea点击:

How to use @Html.CheckBoxFor() while dealing with a List<ViewModel>

c#asp.net-mvc-3razorhtml-helper

提问by kbvishnu

This is my View. How to use CheckboxFor():

这是我的观点。如何使用 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.RoleAccessis a List<RoleAccessViewModel>and I want to create checkbox using @Html.CheckBoxFor().

Model.RoleAccess是一个List<RoleAccessViewModel>,我想使用@Html.CheckBoxFor().

This is my RoleAccessViewModel

这是我的 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?

假设我在列表中有 5 个项目,那么我需要 5 个 ID=menuID 和 Text = menuDisplayName 的复选框。我怎样才能做到这一点?

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 IsEnabledfrom stringto bool. The checkbox works. But Label prints only MenuDisplayNameinstead of values . Can any one helps ?

但是在将IsEnabledfrom的类型更改stringbool. 复选框有效。但是 Label 只打印MenuDisplayName而不是 values 。任何人都可以帮忙吗?

采纳答案by Madman

Change foreach with for

用 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
}

回答by AliR?za Ad?yah?i

If I undestood right, You mean this? And IsEnabledshould bool type

如果我理解正确,你的意思是这个?并且IsEnabled应该布尔类型

model

模型

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; }
}

view

看法

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

回答by Darin Dimitrov

CheckBoxForworks with boolean properties only. So the first thing you need to do is to modify your view model in order to include a boolean property indicating whether the record was selected:

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; }
}

and then I would recommend replacing your foreachloop with an editor template:

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

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

and finally write the corresponding editor template which will automatically be rendered for each element of the RolesAccess collection (~/Views/Shared/EditorTemplates/RoleAccessViewModel.cshtml):

最后编写相应的编辑器模板,该模板将自动为 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)

回答by vbjay

I know it is pretty old but

我知道它很旧但是

In your model use DataAnotations Display Attribute

在您的模型中使用DataAnotations 显示属性

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