模型回传数据绑定类型列表℃的控制器动作参数; T> [英] Model binding postback data to a controller action parameter of type List<T>

查看:135
本文介绍了模型回传数据绑定类型列表℃的控制器动作参数; T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类型的强类型视图

 列表<名单,LT; MyViewModelClass>>

外部列表总会有的两个列表名单,LT; MyViewModelClass> 。对于两个外部列表我要显示一组复选框。每一组可以有选择任意号码。

我的视图模型类类似于此:

 公共类MyViewModelClass
{
    公共区域面积{搞定;组; }    公共BOOL IsGeneric {搞定;组; }    公共字符串code {搞定;组; }    公共BOOL {器isChecked获得;组; }
}

所以最终的观点看起来是这样的:


请选择那些适用于:

第一套选择:


  • <大骨节病> X 选项1

  • <大骨节病> X 选项2

  • <大骨节病> X 选项3


二组选择:


  • <大骨节病> X 第二个选项1

  • <大骨节病> X 第二个选项2

  • <大骨节病> X 第二个选项3

  • <大骨节病> X 第二个选项4



复选框应该显示 MyViewModelClass.Area.Name ,其价值应与 MyViewModelClass.Area.Id 。选中状态固然与 MyViewModel.IsChecked

问题

我不知道我应该如何使用 Html.CheckBox() Html.CheckBoxFor()助手来显示我的复选框?我必须得到这些值回上当然是发的服务器。

我想有我的控制器的动作像其中的一个:

 公众的ActionResult ConsumeSelections(列表&LT;名单,LT; MyViewModelClass&GT;&GT;数据)
{
    // 处理数据
}公众的ActionResult ConsumeSelections(列表&LT; MyViewModelClass&gt;首先,列表与LT; MyViewModelClass&gt;第二个)
{
    // 处理数据
}

如果它使事情变得更简单,我可以做一个独立的视图模型类型,如:

 公共类选项
{
    公开名单&LT; MyViewModelClass&GT;首先{搞定;组; }    公开名单&LT; MyViewModelClass&GT;二{搞定;组; }
}

以及改变我的控制器动作的第一个版本:

 公众的ActionResult ConsumeSelections(选择数据)
{
    // 处理数据
}


解决方案

解决方案

原来,这不是那么复杂的。所有你所要做的就是适当地命名控制,一切都将被绑定在一起,它应该是。所以。无论你的控制,应始终将它们命名,如:

  NAME =第一[0] .propName
NAME =第[1] .propName
NAME =第[2] .propName
NAME =第[3] .propName
...// 要么
NAME =第一[0]。数据[0] .property
NAME =第一[0]。数据[1] .property
NAME =第一[0]。数据[2] .property
...
NAME =第[1]。数据[0] .property
NAME =第[1]。数据[1] .property
...

所有这些将得到势必列表&LT;&SOMETYPE GT;第一个控制器操作参数(第二个具有集合中的另一个集合)。

非常重要的注意事项结果
如果添加/删除使用JavaScript动态这些控件,你必须确保,该指数是连续的从0开始的,并没有任何间隙。你将有一个集合中元素的个数不断变化的一个很好的工作程序。

您可以的我的博客帖子为好。

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
}

解决方案

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:

name="first[0].propName"
name="first[1].propName"
name="first[2].propName"
name="first[3].propName"
...

// or
name="first[0].data[0].property"
name="first[0].data[1].property"
name="first[0].data[2].property"
...
name="first[1].data[0].property"
name="first[1].data[1].property"
...

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.

这篇关于模型回传数据绑定类型列表℃的控制器动作参数; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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