ASP.NET MVC2:DropDownListFor 无法在多项选择中正常工作模型绑定器
我试图弄清楚这个 DropDownListFor 是如何工作的,但没有成功。
我的控制器创建一个 SelectListItems 列表,其中放置在数据库中找到的所有组:
viewModel.Groups = LoadGroups(viewModel.User.AssociatedGroups);
这就是方法:
private IList<SelectListItem> LoadGroups(List<String> associatedGroups)
{
var groups = _SecurityService
.LoadGroups()
.Select(e => new SelectListItem
{
Selected = associatedGroups.Contains<System.String>(e.Id.ToString()),
Value = e.Id.ToString(),
Text = e.Name
}).ToList();
return (groups);
}
如您所见,如果列表中有关联的组,我会设置 Selected 元素。 我将此列表放入自定义视图模型的字段(组)中:
public class UsersViewModel
{
public UsersViewModel()
{
this.Groups = new List<SelectListItem>();
}
public Models.User User { get; set; }
public IList<SelectListItem> Groups { get; set; }
}
并将 UsersViewModel 发送到视图。我使用这段代码构建一个具有多项选择的下拉菜单:
<%=Html.DropDownListFor(m => m.User.AssociatedGroups, (List<System.Web.Mvc.SelectListItem>)Model.Groups, new { @class = "dropDownGroups", multiple = "multiple" })%>
AssociatedGroups 是 Users 类的一个字段(它是我的视图模型的成员):
public List<String> AssociatedGroups { get; set; }
这里没有什么特别的。 如果我使用此代码,我可以看不到所选下拉列表的元素(并且它们具有属性集,我已经仔细检查过),但是当我发布表单时,我可以将选择与 AssociatedGroups 绑定。 如果我用字符串更改 AssociatedGroups (User 类的字段):
public String AssociatedGroups { get; set; }
我有相反的行为:
我可以看到下拉列表的元素被选中,但当我发布表单时,没有绑定,或者更好,只有一个元素被绑定。 我花了一天的大部分时间试图找出问题所在,并且尝试了不同的组合,但似乎都不起作用。 有人可以尝试帮助我吗?
谢谢。
I am trying to figure out how this DropDownListFor works but with no success.
My controller creates a list of SelectListItems where I put all the groups found in the database:
viewModel.Groups = LoadGroups(viewModel.User.AssociatedGroups);
this it the method:
private IList<SelectListItem> LoadGroups(List<String> associatedGroups)
{
var groups = _SecurityService
.LoadGroups()
.Select(e => new SelectListItem
{
Selected = associatedGroups.Contains<System.String>(e.Id.ToString()),
Value = e.Id.ToString(),
Text = e.Name
}).ToList();
return (groups);
}
As you can see I set the Selected element if there are associated groups in the list.
I put this list in a field (Groups) of my custom viewmodel:
public class UsersViewModel
{
public UsersViewModel()
{
this.Groups = new List<SelectListItem>();
}
public Models.User User { get; set; }
public IList<SelectListItem> Groups { get; set; }
}
and send UsersViewModel to the view. I use this code to build a dropdown with multi-selection:
<%=Html.DropDownListFor(m => m.User.AssociatedGroups, (List<System.Web.Mvc.SelectListItem>)Model.Groups, new { @class = "dropDownGroups", multiple = "multiple" })%>
AssociatedGroups is a field the class Users (which is a member of my viewmodel):
public List<String> AssociatedGroups { get; set; }
There's nothing peculiar here.
If I use this code I can I can't see the elements of the dropdown selected (and they have the attribute set, I've double checked), but I can bind the selections with AssociatedGroups when I post the form.
If I change AssociatedGroups (field of the User class) with a string:
public String AssociatedGroups { get; set; }
I have the opposite behaviour:
I can see the elements of the dropdown checked but when I post the form there's no binding, or better, only one element is bound.
I've spend most of my day trying to figure out what the problem and I've tried different combinations but none of them seem to work.
Is there anyone who can try to help me?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的视图模型上需要两个属性:一个将包含选定的组 ID,另一个将包含列表:
然后您将使用
ListBoxFor
帮助器来允许多种选择:现在假设以下内容视图模型传递给视图:
然后在视图上最后两个元素将被自动预选。
You need two properties on your view model: one that will contain the selected group ids and one that will contain the list:
and then you would use the
ListBoxFor
helper to allow for multiple choices:Now assuming the following view model is passed to the view:
Then on the view the last two elements will be automatically preselected.