通过一个动作传递复选框的选择 [英] Passing checkbox selection through to an action

查看:169
本文介绍了通过一个动作传递复选框的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于用户一堆复选框来指定希望在网格上看到哪些列:

I have a bunch of checkboxes that are used for the user to specify which columns they want to see on a grid:

目前,每个复选框有它自己的密钥(这基本上是它的标签名),这样的声明在我看来:

At the moment, each checkbox has it's own key (which is essentially it's label name) and is declared like this in my view:

@Html.CheckBox(column.Key,
               (Request.Form[column.Key] == null ? true : 
                Request.Form[column.Key].Contains("true")),
               new { @class = "columnSelector" })

@Html.Label(column.HeaderText)

问题是,我得从表单中收集的价值观在我的行动,否则我将不得不有一个布尔参数为每列选择复选框。另外,我想我能说出他们所有的columnselection'什么的,然后将它传递给我的行动值的数组,但后来我失去了价值的情况下,因为我没有列项。

The issue is that I have to get the values from the form collection in my action, as otherwise I would have to have a bool parameter for every column selector checkbox. Alternatively, I thought I could name them all 'columnselection' or something and then it would be passed to my action as an array of values, however then I lose the context of the value since I don't have the column key.

我不希望创建为每个复选框的属性一个视图模型为这个功能用在其他屏幕不同的栏目,我想保持它通用的。

I don't want to create a viewmodel with a property for each checkbox as this functionality is used on other screens with different columns and I would like to keep it generic.

我如何能做到这一点列选择的东西,在一个整洁和简单的方式有什么想法?

Any thoughts on how I could achieve this column selection stuff in a clean and simple way?

推荐答案

Html.CheckBox 方法,有一个奇怪的实现。复选框将有一个独特的名称,并且基本上是一个的true / false 值。这是为了让该值将很容易地映射到布尔参数的动作。

The Html.CheckBox method has a strange implementation. Checkboxes will have a unique name, and essentially a true/false value. This is so that the value will easily map to a bool parameter in an Action.

正如您已经明显注意到了,这使得一个棘手的情况,当你的复选框是动态的。

As you have obviously noticed, this makes for a tricky situation when your checkboxes are dynamic.

解决方案是生成自己的复选框,所有使用通用名称,并具有独特的秒。这些将映射非常漂亮到你的行动!

The solution is to generate your own checkboxes, all using a common name and having unique values. These will map very nicely into your Action!

<一个href="http://stackoverflow.com/questions/220020/how-to-handle-checkboxes-in-asp-net-mvc-forms/220073#220073">See这个问题太,一些伟大的例子和解释的。

下面是所需的操作:

public ActionResult SetSelectedColumns(string[] selectedColumns) {
    // selectedColumns will contain the keys of all the checked columns.
}

和这里是如何让你的HTML正确映射:

And here's how to get your HTML to map correctly:

<input type="checkbox" name="selectedColumns" value="@column.Key" @(column.Selected ? "checked='checked'" : "") />

我强烈建议把这个功能集成到一个扩展方法,这样你也可以将的ModelState htmlAttributes 。这是我的未经测试的尝试:

I highly recommend putting this functionality into an extension method, so that you can also incorporate ModelState and htmlAttributes. Here's my untested attempt:

    public static HtmlString CheckBoxAlt(this HtmlHelper html, string name, string value, bool selected, object htmlAttributes)
    {
        var tag = new TagBuilder("input");
        if (htmlAttributes != null)
            tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        tag.MergeAttribute("type", "checkbox");
        tag.MergeAttribute("name", name);
        tag.MergeAttribute("value", value);
        // Determine if this check should be selected:
        var state = html.ViewData.ModelState[name];
        if (state != null)
        {
            var values = (string[])state.Value.ConvertTo(typeof(string[]));
            selected = Array.IndexOf(values, value) != -1;
        }
        if (selected)
            tag.MergeAttribute("checked", "checked");

        return new HtmlString(tag.ToString(TagRenderMode.SelfClosing));
    }

这篇关于通过一个动作传递复选框的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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