ASP.NET MVC 2.0-IList< T>复选框 [英] ASP.NET MVC 2.0 - IList<T> CheckBoxes

查看:44
本文介绍了ASP.NET MVC 2.0-IList< T>复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理此问题的最佳方法是什么?

What is the best way to handle this:

class Option {
    int id;
    string name;
}

class QuoteItem
{
     IList<Option> options;
}

class QuoteViewModel {
     IList<Option> allOptions;
     QuoteItem quoteItem;
}

基本上,我在allOptions中具有所有可用的选项.我想有把一个复选框另一(即使它只是它的)到列表,当它被选中.我将如何完成?最好是IList<bool>并在事后将其绑定吗?

Basically, I have all the available options in allOptions. I want to have a checkbox that puts another Option (even if its just its id) into the QuoteItem.options list when it is checked. How would I accomplish this? Would it best be an IList<bool> and bind it after the fact?

推荐答案

我建议您看看此

I suggest you take look at this blog entry from Phil Haack about model binding to a list

根据您的情况,您可以对IEnumerable<int> options使用简单的模型绑定,其中的值将是所选选项的ID.

For your situation you can use simple model binding to a IEnumerable<int> options, where the values will be the id of your selected options.

您的输入视图将如下所示:

your input view will then look something like this:

<form method="post" action="/QuoteItems/SetOptions">
        <input type="hidden" name="options" value="1" />
        <input type="hidden" name="options" value="4" />
        <input type="hidden" name="options" value="2" />
        <input type="hidden" name="options" value="8" />
        <input type="submit" />
    </form>

隐藏的输入包含您选择的optionId的注释名称属性,每个隐藏的输入都相同.默认的模型绑定器可以将此绑定到整数列表.

The hidden inputs contain your selected optionId's, note name attribute which is the same for each hidden input. The default model binder can bind this to a list of integers.

下一步,您需要根据是否在"all-options"选择控件中选择了某个项目,在客户端添加/删除隐藏的选项输入.

The thing you need to do next is adding / removing a hidden options input at client side depending on whether an item is selected in your "all-options" select control.

这篇关于ASP.NET MVC 2.0-IList&lt; T&gt;复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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