ASP.NET MVC 2.0-IList< T>复选框 [英] ASP.NET MVC 2.0 - IList<T> CheckBoxes
问题描述
处理此问题的最佳方法是什么?
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< T>复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!