模型将回发数据绑定到类型为 List的控制器操作参数。
我有一个强类型视图,
List<List<MyViewModelClass>>
外部列表总是有两个 List
列表。对于两个外部列表中的每一个,我想显示一组复选框。每组可以有任意数量的选择。
我的视图模型类看起来与此类似:
public class MyViewModelClass
{
public Area Area { get; set; }
public bool IsGeneric { get; set; }
public string Code { get; set; }
public bool IsChecked { get; set; }
}
因此最终视图将类似于:
请选择适用的:
第一组选择:
- x 选项 1
- x 选项 2
- x 选项 3
- 等等
第二组选择:
- x 第二个选项 1
- x 第二个选项 2
- x 第二个选项 3
- x 第二个选项 4
- 等等。
复选框应显示 MyViewModelClass.Area.Name
,其值应与 <代码>MyViewModelClass.Area.Id。 Checked 状态当然与 MyViewModel.IsChecked 相关。
问题
我想知道应该如何使用 Html.CheckBox()
或 Html.CheckBoxFor()
帮助程序来显示我的复选框?当然,我必须在回发时将这些值返回到服务器。
我希望我的控制器操作像以下之一:
public ActionResult ConsumeSelections(List<List<MyViewModelClass>> data)
{
// process data
}
public ActionResult ConsumeSelections(List<MyViewModelClass> first, List<MyViewModelClass> second)
{
// process data
}
如果它使事情变得更简单,我可以制作一个单独的视图模型类型,例如:
public class Options
{
public List<MyViewModelClass> First { get; set; }
public List<MyViewModelClass> Second { get; set; }
}
以及将我的控制器操作的第一个版本更改为:
public ActionResult ConsumeSelections(Options data)
{
// process data
}
I have a strong type view of type
List<List<MyViewModelClass>>
The outer list will always have two lists of List<MyViewModelClass>
. For each of the two outer lists I want to display a group of checkboxes. Each set can have an arbitrary number of choices.
My view model class looks similar to this:
public class MyViewModelClass
{
public Area Area { get; set; }
public bool IsGeneric { get; set; }
public string Code { get; set; }
public bool IsChecked { get; set; }
}
So the final view will look something like:
Please select those that apply:
First set of choices:
- x Option 1
- x Option 2
- x Option 3
- etc.
Second set of choices:
- x Second Option 1
- x Second Option 2
- x Second Option 3
- x Second Option 4
- etc.
Checkboxes should display MyViewModelClass.Area.Name
, and their value should be related to MyViewModelClass.Area.Id
. Checked state is of course related to MyViewModel.IsChecked
.
Question
I wonder how should I use Html.CheckBox()
or Html.CheckBoxFor()
helper to display my checkboxes? I have to get these values back to the server on a postback of course.
I would like to have my controller action like one of these:
public ActionResult ConsumeSelections(List<List<MyViewModelClass>> data)
{
// process data
}
public ActionResult ConsumeSelections(List<MyViewModelClass> first, List<MyViewModelClass> second)
{
// process data
}
If it makes things simpler, I could make a separate view model type like:
public class Options
{
public List<MyViewModelClass> First { get; set; }
public List<MyViewModelClass> Second { get; set; }
}
As well as changing my first version of controller action to:
public ActionResult ConsumeSelections(Options data)
{
// process data
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,我创建一个类型(模型)来“建模”我想要创建的视图。
即
编辑:
看看这篇文章,因为它正是你想要什么!
Generally I create a type (Model) that "models" the view that I want to create.
i.e.
Edit:
Have a look at this post, as it's exactly what you want!
解决方案
事实证明这根本没有那么复杂。您所要做的就是适当地命名您的控件,所有内容都会按其应有的方式绑定在一起。所以。无论您的控件是什么,它们都应该始终命名为:
所有这些都将绑定到
List;第一个
控制器操作参数(第二个控制器在集合内有另一个集合)。非常重要的说明
如果使用 Javascript 动态添加/删除这些控件,则必须确保索引从 0 开始连续,并且没有任何间隙。您将拥有一个很好的工作应用程序,其中集合中的元素数量是动态的。
您可以在我的博客中阅读相关内容也发布。
Solution
It turns out this is not so complicated at all. All you have to do is name your controls appropriately and everything will be bound together as it should be. So. Whatever your controls, they should always be named like:
All these will get bound to
List<SomeType> first
controller action parameter (the second one has another collection inside a collection).Very important note
If you add/remove these controls dynamically using Javascript, you have to make sure, that indexes are consecutive starting from 0, and not having any gaps. And you will have a nice working app with dynamic number of elements in a collection.
You can read about it in my blog post as well.