使用MVC中的Razor传回多个选定的复选框选项 [英] Pass back multiple selected Checkbox Options using Razor in MVC

查看:102
本文介绍了使用MVC中的Razor传回多个选定的复选框选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC填充一组复选框.这些复选框会根据用户在上一个视图中选择的内容而变化.我正在使用Razor.我认为:

I am using ASP.NET MVC to populate a group of checkboxes. These checkboxes change depending on what the user selects on the previous view. I am using Razor. In the view I have:

    @foreach (var skills in Model.SkillsAvailable)
    {
        <input name="pack" type="checkbox" value="@skills" /> @skills<br />
    }

    @Html.HiddenFor(m => m.SkillsSelected, new { @class = "hidden" })

我还有其他一些看起来几乎与此相同,但都是单选单选按钮.它由jQuery起作用,它在更改时采用选定的选项并填充HiddenFor.HiddenFor在提交时传递给控件.没问题.

I have a few others that look nearly identical to this but are single selection radio buttons. It functions by jQuery taking the selected option on change and populating the HiddenFor. The HiddenFor is passed to the control upon submit. No issues with that.

但是对于我的复选框,我不确定如何将多个项目寄回.如果此列表填充了10个项目,而用户只能选择2个,我如何将它们添加到List< string>选择技能?

But for my checkbox, I'm not sure how to send multiple items back. If this list populates 10 items, and the user can only select 2, how do I add them to List<string> SkillsSelected?

我的第一个直觉是我可以使用另一个foreach循环以某种方式添加(例如,已选择的foreach,添加到列表中).但是我不知道它是什么样子,而且Google搜索除了使用bool之外没有显示任何内容,如果每次使用的列表相同,这是有意义的,但是如果列表中有5到30个选项(取决于选项),则没有意义先前的选择.

My first instinct is that I can add somehow using another foreach loop (like foreach that is selected, add to a list). But I have no idea what that looks like, and Google searching isn't bringing anything up except for using bools, which makes sense if the list is the same every time, but not if the list has between 5 and 30 options depending on the previous selection.

目前,我没有任何jQuery控制此部分,因此没有相应的代码.我的控制器正在很好地填充复选框列表,但没有处理其他任何事情.

At this point in time, I don't have any jQuery controlling this section, so I don't have code for it. My controller is populating the list of checkboxes just fine, but not handling anything else.

我模型中唯一重要的属性就是这些,并且在必要时更改它们没有问题.

The only properties in my model that matters are these, and I have no problem changing them in necessary.:

    public List<string> SkillsAvailable { get; set; }
    public List<string> SkillsSelected { get; set; }

推荐答案

在视图中使用列表的技巧(剃刀)是使用 for 循环而不是 foreach .这样,您的html元素将被视为数组.这是您的情况的示例:

The trick with lists in views (razor) is to use for loops instead of foreach. With this, your html elements will be treated as arrays. Here an example for your case:

模型更改,将SkillsSelected属性列表作为布尔值的持有者

A change in the model, have the SkillsSelected property list as holder of the boolean values

public List<string> SkillsAvailable { get; set; }
public List<bool> SkillsSelected { get; set; }

然后,在视图上,使用 for 循环

Then, on the view, use for loop

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    for (int i = 0; i < Model.SkillsAvailable.Count; i++)
    {
        @Html.CheckBoxFor(m => Model.SkillsSelected[i]) @Model.SkillsAvailable[i] <br />
        @Html.HiddenFor(m => Model.SkillsAvailable[i])
    }
    <input type="submit" value="Submit" />
}

作为参考,这是一个有关如何在控制器中传递和接收模型的示例

And as a reference, here an example on how to pass and receive the model in the controller

public ActionResult Index()
{
    var model = new Model1()
    {
        SkillsAvailable = new List<string>() { "Uno", "Dos", "Tres" }
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(Model1 model)
{
    return View(model);
}

这篇关于使用MVC中的Razor传回多个选定的复选框选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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