ASP.NET MVC 2.0 - IList的的CheckBox
-
21-09-2019 - |
题
什么是处理的最佳方式:
class Option {
int id;
string name;
}
class QuoteItem
{
IList<Option> options;
}
class QuoteViewModel {
IList<Option> allOptions;
QuoteItem quoteItem;
}
基本上,我有在allOptions
所有可用的选项。我想有另一张Option
(即使它只是它的id
)到QuoteItem.options
列表时检查的复选框。我将如何做到这一点?将它最好是一个IList<bool>
并且将其绑定在事后?
解决方案
我建议你去看看这个从菲尔哈克博客条目了解模型绑定到一个列表
有关你的情况,你可以使用简单的模型绑定到IEnumerable<int> options
,其中的值将是你选择的选项的ID。
您输入视图,然后会是这个样子:
<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的,音名属性,是每个隐藏输入相同。默认模型粘合剂可结合这对整数列表。
您下一步需要做的事情是添加/在客户端取决于项目是否在您的“所有选项”选择控制选择删除一个隐藏的选项输入。
不隶属于 StackOverflow