保持复选框在ASP.NET MVC的动态列表的状态 [英] Maintain state of a dynamic list of checkboxes in ASP.NET MVC

查看:98
本文介绍了保持复选框在ASP.NET MVC的动态列表的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为PropertyFeature类只包含PropertyFeatureID和说明。它是通过LINQ创建映射到SQL Server数据库表的SQL合适的模式。一个例子例如/行是:

I have a class called "PropertyFeature" which simply contains PropertyFeatureID and Description. It's a proper model created through LINQ to SQL mapped to an SQL Server database table. An example instance/row would be:

PropertyFeatureID: 2
Description: "Swimming Pool"

行(PropertyFeatures)的数量当然可以增大和缩小,所以我想动态呈现复选框的列表,以便用户可以选择其中任何一个。
我可以动态地呈现复选框很轻松了,喜欢的东西:

The number of rows (PropertyFeatures) can of course grow and shrink, and so I want to dynamically render a list of checkboxes so that the user can select any of them. I can dynamically render the Checkboxes easily enough, with something like:

<%foreach (var Feature in (ViewData["Features"] as IEnumerable<MySolution.Models.PropertyFeature>)) { %>
<%=Html.CheckBox("Features", new { @id = Feature.PropertyFeatureID, @value = Feature.PropertyFeatureID })%><label for="Feature<%=Feature.PropertyFeatureID%>"><%=Feature.Description%></label>
<%}%>

我指定每个复选框的ID和呈现匹配的标签,使用户可以直观地点击标签,切换复选框 - 这伟大工程

I specify the ID for each checkbox and render the matching label so that the user can intuitively click the label and toggle the checkbox - that works great.

我设置了​​CheckBox的名,以特色,让所有复选框使具有相同的名称,以及MVC模型绑定桩他们到时调用的形式发布功能单个集合。这工作得很好。

I set the CheckBox's "name" to "Features" so all the checkboxes render with the same name, and the MVC Model Binder piles them into a single collection called "Features" when the form is posted. This works nicely.

一旦提交表单,我用的检查值,并存储它们,所以我需要实际的整数值,所以我知道这PropertyFeature选择,而不仅仅是一堆布尔和字段名。所以,理想情况下,我希望它是一个数组或一个集合,很容易的工作。

Once the form is submitted, I use the checked values and store them, so I need the actual integer values so I know which PropertyFeature is selected, not just a pile of Booleans and field names. So ideally, I want it as an array or a collection that's easy to work with.

我能够从我的控制器方法中检索选定值时,单击该按钮,因为我指定的参数为int []功能。

I am able to retrieve the selected values from within my Controller method when the button is clicked because I have specified the parameter as int[] Features.

但问题的是,它不保持状态。也就是说,当我点击提交按钮,页面重新加载(与形式再次显示)我希望所有的动态复选框以保留其检查状态(或没有)。所有这一切我已经与 Html.DropDownList 创建的其他领域和 Html.TextBox 都有各自的状态没有成功在所有以相同的形式在同一页上的问题。

But the problem is that it doesn't maintain state. That is, when I click the submit button and the page reloads (with the form again displayed) I want all of the dynamic checkboxes to retain their checked status (or not). All of the other fields that I've created with Html.DropDownList and Html.TextBox all maintain their states successfully no problems at all on the same page in the same form.

我已经花了几个小时阅读所有关于类似问题的其他线程和文章,并有很多的谈话关于使用的ICollection 的IDictionary 来捆绑东西,并​​包括为每个项目一个布尔值,以便它能够保持的复选框的状态。但我没有100%的把握如何使用,在我个人的例子的情况下。我想保持该解决方案非常简单,并没有code起来的新类网页只是为了保持我的复选框的状态。

I have spent hours reading all of the other threads and articles on similar issues and there is a lot of talk about using ICollection and IDictionary to bundle things up and include a Boolean value for each item so that it can maintain the checkbox state. But I don't 100% grasp how to use that in the context of my own personal example. I would like to keep the solution really simple and not have to code up pages of new classes just to maintain my checkbox state.

什么是做到这一点的清洁和适当的方式?

What is the cleanest and proper way to do this?

推荐答案

我得到了它的工作后,用得多的各种不同的方法玩了。

I got it working after much playing around with the various different approaches.

在视图:

<%string[] PostFeatures = Request.Form.GetValues("Features");%>
<% foreach (var Feature in (ViewData["AllPropertyFeatures"] as 
                             IEnumerable<MySolution.Models.PropertyFeature>)) 
   { %>
    <input type="checkbox" name="Features"
        id="Feature<%=Feature.PropertyFeatureID.ToString()%>" 
        value="<%=Feature.PropertyFeatureID%>" 
        <%if(PostFeatures!=null) 
          {
             if(PostFeatures.Contains(Feature.PropertyFeatureID.ToString()))
             { 
                Response.Write("checked=\"checked\""); 
             }
          }
        %> />
    <label for="Feature<%=Feature.PropertyFeatureID%>">
          <%=Feature.Description%></label>   <%
   }  %>

在接收控制器方法:

public ActionResult SearchResults(int[] Features)

此方法具有许多优点:


  • 允许点击标签来切换相应的复选框(可用性)。

  • 允许控制器方法来接收整数的超级整齐排列,其中仅包含已选择的整数 - 而不是一个整体的其他一堆这是未选或含有虚假/ null的项目/空白/ 0等

  • 当包含表单的页面重新加载,即用户的选择被保留
  • 保留该复选框的选中状态。

  • 从默认的ASP.Net MVC Html.CheckBox帮手,没有创建随机/杂散类型=隐藏输入字段 - 我知道它的一个很好的理由,但在这种情况下,我不要求他们为我只想知道哪个ID已经被选择并为那些是在一个单一的,整齐INT []。

  • 来实现这么简单的东西需要额外的服务器端臃肿班,助手和其他乱七八糟的快乐没有群众。

我会推荐这种方法对于任何人想要一个动态的复选框列表最干净的/自由膨胀的解决方案,你需要的ID和你只是想正事!

I would recommend this approach for anyone wanting the cleanest / bloat-free solution for a dynamic checkbox list where you need the IDs and you just want to get down to business!

这篇关于保持复选框在ASP.NET MVC的动态列表的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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