MVC3 如何将多个复选框绑定到 ViewModel 中的 1 个属性

发布于 2024-12-13 12:01:22 字数 660 浏览 1 评论 0原文

我需要显示一个复选框列表,可以选中多个复选框。

当用户点击提交时,这些复选框的值需要进入 ViewModel 中的属性...这是我到目前为止所得到的...

public class RegisterModel
{
    public List<string> Roles { get; set; }
    public List<RoleModel> SelectedRoles { get; set; }    
}
public class RoleModel
{
    public string RoleName { get; set; }
}

在视图中我正在尝试执行此操作...

@foreach (var role in Model.Roles)
{
    @Html.CheckBoxFor(m => m.SelectedRoles, role.RoleName)@role.RoleName
}

我收到以下错误:

CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'bool'

谁能告诉我我做错了什么?

I need to display a list of checkboxes, which more than one can be checked.

When the user hits submit, the value of these checkboxes need to go into a property in the ViewModel...this is what I got so far...

public class RegisterModel
{
    public List<string> Roles { get; set; }
    public List<RoleModel> SelectedRoles { get; set; }    
}
public class RoleModel
{
    public string RoleName { get; set; }
}

In the view I am trying to do this...

@foreach (var role in Model.Roles)
{
    @Html.CheckBoxFor(m => m.SelectedRoles, role.RoleName)@role.RoleName
}

I get the following error:

CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'bool'

Can someone tell me what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2024-12-20 12:01:22

简单:调整视图模型以满足您的视图要求(即显示某些角色的复选框列表),使用编辑器模板并避免在视图中编写循环。

所以:

视图模型:

public class RegisterModel
{
    public List<RoleModel> Roles { get; set; }
}

public class RoleModel
{
    public string RoleName { get; set; }
    public bool Selected { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new RegisterModel
        {
            Roles = new[]
            {
                new RoleModel { RoleName = "administrator" },
                new RoleModel { RoleName = "developer" },
                new RoleModel { RoleName = "janitor :-)" },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(RegisterModel model)
    {
        // at this stage the model will contain all the 
        // information you need
        return View(model);
    }
}

视图(~/Views/Home/Index.cshtml):

@model RegisterModel

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Roles)
    <button type="submit">OK</button>
}

编辑器模板(~/Views/Home/EditorTemplates/RoleModel.cshtml):

@model RoleModel

<div>
    @Html.HiddenFor(x => x.RoleName)
    @Html.CheckBoxFor(x => x.Selected)
    @Html.LabelFor(x => x.Selected, Model.RoleName)
</div>

Simple: adapt your view models to match your views requirement (which is to show a list of checkboxes for some roles), use editor templates and avoid writing loops in your views.

So:

View model:

public class RegisterModel
{
    public List<RoleModel> Roles { get; set; }
}

public class RoleModel
{
    public string RoleName { get; set; }
    public bool Selected { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new RegisterModel
        {
            Roles = new[]
            {
                new RoleModel { RoleName = "administrator" },
                new RoleModel { RoleName = "developer" },
                new RoleModel { RoleName = "janitor :-)" },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(RegisterModel model)
    {
        // at this stage the model will contain all the 
        // information you need
        return View(model);
    }
}

View (~/Views/Home/Index.cshtml):

@model RegisterModel

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Roles)
    <button type="submit">OK</button>
}

Editor template (~/Views/Home/EditorTemplates/RoleModel.cshtml):

@model RoleModel

<div>
    @Html.HiddenFor(x => x.RoleName)
    @Html.CheckBoxFor(x => x.Selected)
    @Html.LabelFor(x => x.Selected, Model.RoleName)
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文