ASP.NET MVC2:DropDownListFor 无法在多项选择中正常工作模型绑定器

发布于 2024-10-15 02:15:10 字数 1752 浏览 0 评论 0原文

我试图弄清楚这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-10-22 02:15:10

您的视图模型上需要两个属性:一个将包含选定的组 ID,另一个将包含列表:

public class UsersViewModel
{
    public IEnumerable<SelectListItem> Groups { get; set; }
    public IEnumerable<string> SelectedGroupIds { get; set; }
}

然后您将使用 ListBoxFor 帮助器来允许多种选择:

<%= Html.ListBoxFor(
    m => m.SelectedGroupIds, 
    new SelectList(Model.Groups, "Value", "Text"), 
    new { @class = "dropDownGroups" }
) %>

现在假设以下内容视图模型传递给视图:

public ActionResult Index()
{
    var model = new UsersViewModel
    {
        // TODO: Use a repository to fetch those values 
        // and map them to the view model
        Groups = new[] 
        {
            new SelectListItem { Value = "1", Text = "group 1" },
            new SelectListItem { Value = "2", Text = "group 2" },
            new SelectListItem { Value = "3", Text = "group 3" },
        },
        // We want to preselect the last two groups in the listbox
        SelectedGroupIds = new[] { "2", "3" }
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(IEnumerable<string> selectedGroupIds)
{
    // Here we will get the list of selected ids
    // when the form containing the listbox is
    // submitted
    ...
}

然后在视图上最后两个元素将被自动预选。

You need two properties on your view model: one that will contain the selected group ids and one that will contain the list:

public class UsersViewModel
{
    public IEnumerable<SelectListItem> Groups { get; set; }
    public IEnumerable<string> SelectedGroupIds { get; set; }
}

and then you would use the ListBoxFor helper to allow for multiple choices:

<%= Html.ListBoxFor(
    m => m.SelectedGroupIds, 
    new SelectList(Model.Groups, "Value", "Text"), 
    new { @class = "dropDownGroups" }
) %>

Now assuming the following view model is passed to the view:

public ActionResult Index()
{
    var model = new UsersViewModel
    {
        // TODO: Use a repository to fetch those values 
        // and map them to the view model
        Groups = new[] 
        {
            new SelectListItem { Value = "1", Text = "group 1" },
            new SelectListItem { Value = "2", Text = "group 2" },
            new SelectListItem { Value = "3", Text = "group 3" },
        },
        // We want to preselect the last two groups in the listbox
        SelectedGroupIds = new[] { "2", "3" }
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(IEnumerable<string> selectedGroupIds)
{
    // Here we will get the list of selected ids
    // when the form containing the listbox is
    // submitted
    ...
}

Then on the view the last two elements will be automatically preselected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文