如何处理在ASP.NET MVC的形式复选框? [英] How to handle checkboxes in ASP.NET MVC forms?

查看:80
本文介绍了如何处理在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.

我有对象的集合,我希望用户选择一个或多个。这对我说:与复选框的形式。我的对象不具有选定的概念,任何(他们基本的POCO的反序列化由一个WCF调用形成)。所以,我做到以下几点:

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.

显然,我失去了一些东西。我认为这是指一经HTML表单内行事集合中的对象是使用更新了心中的​​想法建的UpdateModel()或通过模型绑定器。

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做奇怪的事情 - 如果你在​​结果页中查看源代码,你会看到有一个&LT;输入类型=隐藏/&GT; 旁边每个复选框,这也解释了你看到的每一个表单元素

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测试版,因为我刚刚试过了。

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

将这个():

<% 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 每个复选框的是相应的对象的GUID。

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

然后邮寄到下列控制器动作(或类似的东西,做一些有用的东西,而不是回复于())

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集合字符串到instantied 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天全站免登陆