ASP.NET MVC NHibernate的 - 绑定多选择列表的IList&LT; T&GT; [英] ASP.NET MVC NHibernate - Bind multi select list to IList<T>
问题描述
我如何能多选下拉列表绑定到一个手动添加中间表的列表属性?
类
公共类系
{
公共虚拟INT标识{搞定;组; }
公共虚拟的IList&LT;&林GT;拉姆塞{搞定;组; }
}公共类Person
{
公共虚拟INT标识{搞定;组; }
公共虚拟的IList&LT;&林GT;拉姆塞{搞定;组; }
}公共类林
{
公共虚拟INT标识{搞定;组; }
公共虚拟部门部门{搞定;组; }
公共虚拟人人{搞定;组; }
}
视图模型
公共类DepartmentCreateEditViewModel
{
公共部门部门{搞定;组; }
公众的IList&LT;&人GT;人{搞定;组; }
}
的ActionResult
公众的ActionResult的Create()
{
//获取所有的人
变种人= repositoryPerson.GetAll(); //创建视图模型
VAR视图模型=新DepartmentCreateEditViewModel(){部=新部门(),人=人}; //显示视图
返回视图(视图模型);
}
创建视图
我尝试添加一个ListBox是这样的。
@ Html.ListBoxFor(型号=&GT; model.Department.Lams,新的SelectList(Model.Persons,ID,姓名),新{@class =形式-controll})
要救我想回去一处对象的对象
[HttpPost]
公众的ActionResult创建(系科)
从下拉列表中(与人)到IList中的结合是行不通的。我应该怎么做呢?这甚至可能?
code埃里克的建议后
创建的ActionResult
[HttpPost]
公众的ActionResult创建(DepartmentCreateEditViewModel viewModelPostBack)
查看
@ Html.ListBoxFor(型号=&GT; model.Department.Lams,新MultiSelectList(Model.Persons,ID,姓名),新{@class =形式-controll})
我会得到什么:
viewModelPostBack
{} EmpLaceMgmt.ViewModels.DepartmentCreateEditViewModel
部门:{} EmpLaceMgmt.Models.Department
人数:空
viewModelPostBack.Department
{} EmpLaceMgmt.Models.Department
ID:0
拉姆塞:计数= 0
生成的HTML看起来像这样
&LT;选择类=表单控制研究ID =Department_Lams多个=多个名=Department.Lams&GT;
&LT;期权价值=1物实施例人&LT; /选项&GT;
&LT; /选择&GT;
您有三个问题。首先,您要绑定到的IList&LT; T&GT;
,但不会工作,因为模型绑定不会知道它应该建立什么样的具体对象满足该... ...有支持的IList&LT很多对象; T&GT;
,所以哪一个
其次,您需要使用 MultiSelectList
,而不是放在你的助手的SelectList
。
三,你是不是您正在使用创建的网页回发一个不同的型号。以及该类型具有非常不同的结构。在您使用创建页面结构,你的数据与命名创建的 Department.Lams
(因为处
是你的ViewModel的一个属性),但您的帖子动作发生当中,粘结剂将寻找简称为对象的处
模式拉姆塞
,而不是 Department.Lams
。
所以,转换你的模型用一个具体类型,如列表&LT;林&GT;
,然后回发到您的视图模型,而不是部门,并提取部出视图模型,并最终改变你的助手这样的:
@ Html.ListBoxFor(型号=&GT; model.Department.Lams,
新MultiSelectList(Model.Persons,ID,姓名),新{@class =表单控制研究})
How can I bind a multi select dropdown list to a list property of a manually added intermediate table?
Classes
public class Department
{
public virtual int Id { get; set; }
public virtual IList<Lam> Lams { get; set; }
}
public class Person
{
public virtual int Id { get; set; }
public virtual IList<Lam> Lams { get; set; }
}
public class Lam
{
public virtual int Id { get; set; }
public virtual Department Department { get; set; }
public virtual Person Person { get; set; }
}
ViewModel
public class DepartmentCreateEditViewModel
{
public Department Department { get; set; }
public IList<Person> Persons { get; set; }
}
ActionResult
public ActionResult Create()
{
// Get all Persons
var persons = repositoryPerson.GetAll();
// Create ViewModel
var viewModel = new DepartmentCreateEditViewModel() { Department = new Department(), Persons = persons };
// Display View
return View(viewModel);
}
Create View
I tried to add a ListBox like this.
@Html.ListBoxFor(model => model.Department.Lams, new SelectList(Model.Persons, "Id", "Name"), new { @class = "form-controll" })
To save the object I want to get back a Department object
[HttpPost]
public ActionResult Create(Department department)
The binding from the dropdown (with persons) to the IList is not working. How am I supposed to do this? Is this even possible?
[Edit] Code after Erik's suggestion
Create ActionResult
[HttpPost]
public ActionResult Create(DepartmentCreateEditViewModel viewModelPostBack)
View
@Html.ListBoxFor(model => model.Department.Lams, new MultiSelectList(Model.Persons, "Id", "Name"), new { @class = "form-controll" })
What I get back:
viewModelPostBack
{EmpLaceMgmt.ViewModels.DepartmentCreateEditViewModel}
Department: {EmpLaceMgmt.Models.Department}
Persons: null
viewModelPostBack.Department
{EmpLaceMgmt.Models.Department}
Id: 0
Lams: Count = 0
The generated HTML looks like this
<select class="form-controll" id="Department_Lams" multiple="multiple" name="Department.Lams">
<option value="1">Example Person</option>
</select>
You have three problems. First, you are trying to bind to an IList<T>
, but that won't work because the model binder won't know what kind of concrete object it should create to satisfy that... There are many objects that support IList<T>
, so which one?
Secondly, You need to use a MultiSelectList
and not a SelectList
in your helper.
Third, you are posting back a different model type than you are using to create your pages. And that type has a very different structure. In the structure that you create your page with, your data is created with the naming of Department.Lams
(because Department
is a property of your ViewModel) but in your Post action takes a Department
model which, the binder would be looking for an object simply called Lams
, not Department.Lams
.
So, convert your models to use a concrete type, such as List<Lam>
, then post back to your ViewModel rather than Department, and extract the department out of the ViewModel, and finally change your helper to this:
@Html.ListBoxFor(model => model.Department.Lams,
new MultiSelectList(Model.Persons, "Id", "Name"), new { @class = "form-controll" })
这篇关于ASP.NET MVC NHibernate的 - 绑定多选择列表的IList&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!