如何处理 ASP.NET MVC 表单中的复选框? [英] How to handle checkboxes in ASP.NET MVC forms?

查看:19
本文介绍了如何处理 ASP.NET MVC 表单中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您最好的选择是搜索较新的问题,或搜索下面的答案以寻找您的特定 MVC 版本,因为这里的许多答案现已过时.

Your best option is to search for newer questions, or to search the answers below looking for your specific version of MVC, as many answers here are obsolete now.

如果您确实找到了适用于您的版本的答案,请确保该答案包含您正在使用的 MVC 版本.
(原题下面开始)

If you do find an answer that works for your version, please make sure the answer contains the version of MVC you are using.
(The original question starts below)

这对我来说似乎有点奇怪,但据我所知,这就是你的做法.

This seems a bit bizarre to me, but as far as I can tell, this is how you do it.

我有一组对象,我希望用户选择其中一个或多个.这对我说带有复选框的表单".我的对象没有任何选择"的概念(它们是通过反序列化 wcf 调用形成的基本 POCO).因此,我执行以下操作:

I have a collection of objects, and I want users to select one or more of them. This says to me "form with checkboxes." My objects don't have any concept of "selected" (they're rudimentary POCO's formed by deserializing a wcf call). So, I do the following:

public class SampleObject{
  public Guid Id {get;set;}
  public string Name {get;set;}
}

在视图中:

<%
    using (Html.BeginForm())
    {
%>
  <%foreach (var o in ViewData.Model) {%>
    <%=Html.CheckBox(o.Id)%>&nbsp;<%= o.Name %>
  <%}%>
  <input type="submit" value="Submit" />
<%}%>

而且,在控制器中,这是我能看到的找出用户检查了哪些对象的唯一方法:

And, in the controller, this is the only way I can see to figure out what objects the user checked:

public ActionResult ThisLooksWeird(FormCollection result)
{
  var winnars = from x in result.AllKeys
          where result[x] != "false"
          select x;
  // yadda
}

首先它很奇怪,其次,对于用户检查的那些项目,FormCollection 将其值列为真假"而不仅仅是真.

Its freaky in the first place, and secondly, for those items the user checked, the FormCollection lists its value as "true false" rather than just true.

显然,我遗漏了一些东西.我认为这是基于这样的想法构建的,即使用 UpdateModel() 或通过 ModelBinder 更新在 html 表单内操作的集合中的对象.

Obviously, I'm missing something. I think this is built with the idea in mind that the objects in the collection that are acted upon within the html form are updated using UpdateModel() or through a ModelBinder.

但是我的对象没有为此设置;这是否意味着这是唯一的方法?还有其他方法吗?

But my objects aren't set up for this; does that mean that this is the only way? Is there another way to do it?

推荐答案

Html.CheckBox 正在做一些奇怪的事情 - 如果您在结果页面上查看源代码,您会看到有一个 <input type="hidden"/> 与每个复选框一起生成,这解释了您为每个表单元素看到的真假"值.

Html.CheckBox is doing something weird - if you view source on the resulting page, you'll see there's an <input type="hidden" /> being generated alongside each checkbox, which explains the "true false" values you're seeing for each form element.

试试这个,它绝对适用于 ASP.NET MVC Beta,因为我刚刚尝试过.

Try this, which definitely works on ASP.NET MVC Beta because I've just tried it.

把它放在视图中而不是使用 Html.CheckBox():

Put this in the view instead of using Html.CheckBox():

<% using (Html.BeginForm("ShowData", "Home")) {  %>
  <% foreach (var o in ViewData.Model) { %>
    <input type="checkbox" name="selectedObjects" value="<%=o.Id%>">
    <%= o.Name %>
  <%}%>
  <input type="submit" value="Submit" />
<%}%>

你的复选框都叫做selectedObjects,每个复选框的value就是对应对象的GUID.

Your checkboxes are all called selectedObjects, and the value of each checkbox is the GUID of the corresponding object.

然后发布到以下控制器操作(或类似的东西,而不是 Response.Write())

Then post to the following controller action (or something similar that does something useful instead of Response.Write())

public ActionResult ShowData(Guid[] selectedObjects) {
    foreach (Guid guid in selectedObjects) {
        Response.Write(guid.ToString());
    }
    Response.End();
    return (new EmptyResult());
}

这个例子只会写出你选中的框的 GUID;ASP.NET MVC 将选中复选框的 GUID 值映射到 Guid[] selectedObjects 参数中,甚至将 Request.Form 集合中的字符串解析为实例化的 GUID 对象,我认为这是不错.

This example will just write the GUIDs of the boxes you checked; ASP.NET MVC maps the GUID values of the selected checkboxes into the Guid[] selectedObjects parameter for you, and even parses the strings from the Request.Form collection into instantied GUID objects, which I think is rather nice.

这篇关于如何处理 ASP.NET MVC 表单中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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