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

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

问题描述

我有一个名为PropertyFeature"的类,它只包含 PropertyFeatureID 和 Description.它是通过 LINQ to SQL 创建的正确模型,映射到 SQL Server 数据库表.示例实例/行将是:

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 的名称"设置为Features",因此所有复选框都以相同的名称呈现,当表单发布时,MVC 模型绑定器将它们堆积到一个名为Features"的集合中.这很好用.

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.DropDownListHtml.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.

我花了几个小时阅读所有其他关于类似问题的线程和文章,并且有很多关于使用 ICollectionIDictionary 来捆绑和包含的讨论每个项目的布尔值,以便它可以保持复选框状态.但我并没有 100% 掌握如何在我自己的个人示例中使用它.我想让解决方案非常简单,而不必为了维护我的复选框状态而编写新类的页面.

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)

这种方法有很多优点:

  • 允许点击标签来切换相应的复选框(可用性).
  • 允许 Controller 方法接收一个超级整洁的整数数组,其中只包含已选择的整数 - 而不是其他一堆未选择或包含 false/null/blank/0 等的项目.
  • 在包含表单的页面重新加载时保留复选框的选中状态,即保留用户的选择.
  • 没有从默认的 ASP.Net MVC Html.CheckBox 助手创建的随机/杂散类型=隐藏输入字段 - 我知道它这样做是有充分理由的,但在这种情况下,我不需要它们,因为我只想要了解哪些 ID 已被选中,以及哪些 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天全站免登陆