从 FormCollection 中获取所有选定的复选框

发布于 2024-09-25 09:56:58 字数 736 浏览 3 评论 0原文

我有一个表单,其中包含一大堆复选框和一些其他类型的控件。我需要检索每个选定复选框的名称。

最好的方法是什么?我可以用 linq 查询来完成吗?

因此,在伪代码中,我希望执行以下操作:

var names = formCollection
               .Where(c => c is Checkbox && c.Checked)
               .Select(c => c.Name);

更新 MVC 提交复选框的方式似乎与普通表单的行为方式不同,因为还呈现了隐藏字段。我在这里找到了详细信息:如何处理 ASP.NET 中的复选框MVC 表单?

不管怎样,我已经在该线程和下面 BuildStarted 的答案的帮助下让它工作了。下面的代码就达到了目的。

var additionalItems = form.AllKeys
       .Where(k => form[k].Contains("true") && k.StartsWith("addItem"))
               .Select(k => k.Substring(7));

I have a form which contains a whole bunch of checkboxes and some other types of control too. I need to retrieve the names of each selected checkbox.

What is the best way to do this? Can I do it with a linq query maybe?

So in pseudocode, I'm looking to do something like this:

var names = formCollection
               .Where(c => c is Checkbox && c.Checked)
               .Select(c => c.Name);

Update It seems the way MVC submits checkboxes is different from how a normal form would behave, as an hidden field is also rendered. I found the details here: How to handle checkboxes in ASP.NET MVC forms?

Anywho, I've got it working with the help of that thread and the answer from BuildStarted below. The following code did the trick.

var additionalItems = form.AllKeys
       .Where(k => form[k].Contains("true") && k.StartsWith("addItem"))
               .Select(k => k.Substring(7));

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

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

发布评论

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

评论(3

后知后觉 2024-10-02 09:56:58

不幸的是,该集合中没有此类信息。但是,如果您在所有复选框前面添加类似 的内容,那么您可以运行类似的查询,

var names = formCollection.AllKeys.Where(c => c.StartsWith("checkbox"));

因为只有选中的值才会回发给您不需要验证它们是否已被检查。

这是仅获取已检查值的一个

var names = formCollection.AllKeys.Where(c => c.StartsWith("test") && 
                        formCollection.GetValue(c) != null &&
                        formCollection.GetValue(c).AttemptedValue == "1");

Unfortunately that type of information isn't available in the collection. However if you prepend all your checkboxes with something like <input type='checkbox' name='checkbox_somevalue' /> then you can run a query like

var names = formCollection.AllKeys.Where(c => c.StartsWith("checkbox"));

Since only the checked values will be posted back you don't need to validate that they're checked.

Here's one that grabs only checked values

var names = formCollection.AllKeys.Where(c => c.StartsWith("test") && 
                        formCollection.GetValue(c) != null &&
                        formCollection.GetValue(c).AttemptedValue == "1");
未央 2024-10-02 09:56:58

这是多年来不活跃的老问题之一,但我偶然发现了它。我的问题是我有一个复选框数组 - 假设名称是“IsValid”并且想要获取每个复选框的状态(我的项目位于 MVC 5 中)。在表单提交上,我执行了表单收集的循环,并得到了值...

if (key.Contains("IsValid"))
                    sV = (string[])collection.GetValue(key.ToString()).RawValue;

因为在表单发布上,隐藏字段值也与选中的复选框一起发布;对于仅选中的复选框,该数组包含一个附加值“false”。为了摆脱那些我使用以下功能;我希望它对某人有所帮助,如果我的方法是错误的,那么更好的解决方案也会对我有帮助!

sV = FixCheckBoxValue(sV);

        private string[] FixCheckBoxValue(string[] sV)
    {
        var iArrayList = new List<string>(sV);

        for (int i = 0; i < iArrayList.Count; i++)
        {
            if (iArrayList[i].ToString() == "true")
            {
                iArrayList.RemoveAt(i + 1);
            }                
        }
        return iArrayList.ToArray();
    }

This is one of the old questions not active for years but I stumbled on it. My problem was that I have an array of check boxes - let's say the name is 'IsValid' and wanted to get the status of each of the check boxes (my project was in MVC 5). On form submit i did the loop of form collection and got the values as...

if (key.Contains("IsValid"))
                    sV = (string[])collection.GetValue(key.ToString()).RawValue;

Since on form post the hidden field value was also posted with the checked check boxes; the array contained one additional value of 'false' for ONLY checked check box. To get rid of those i used following function; I hope that it helps somebody and if my approach is wrong then a better solution would be helpful to me as well!

sV = FixCheckBoxValue(sV);

        private string[] FixCheckBoxValue(string[] sV)
    {
        var iArrayList = new List<string>(sV);

        for (int i = 0; i < iArrayList.Count; i++)
        {
            if (iArrayList[i].ToString() == "true")
            {
                iArrayList.RemoveAt(i + 1);
            }                
        }
        return iArrayList.ToArray();
    }
时间你老了 2024-10-02 09:56:58

在回答有关复选框数组的帖子时,您最初可以按名称过滤掉复选框。

var keys = formCollection.AllKeys.Where(x => x.StartsWith("IsValid");

然后你可以将它们作为一个集合进行循环。

Answering the post about the array of checkboxes, you could initially filter out the checkboxes by name.

var keys = formCollection.AllKeys.Where(x => x.StartsWith("IsValid");

Then you could just loop through them as a collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文