MVC 5 MultiSelectList选定值不工作 [英] MVC 5 MultiSelectList Selected Values Not Working

查看:438
本文介绍了MVC 5 MultiSelectList选定值不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示的可能选择一个视图列表框。然而,由于选择了未示出的用户的角色(多个)。在我UserViewModel角色集合包含角色的完整列表,并收集AspNetRoles只包含用户所属的角色。我已经试过多个例子/变化却迟迟没有显示为选中状态。我卡住了。

视图模型:

 公共类UserViewModel
{
    公共字符串ID {搞定;组; }    公共字符串电子邮件{获得;组; }    公众的ICollection< AspNetRole>角色{搞定;组; }    公共虚拟的ICollection< AspNetRole> AspNetRoles {搞定;组; }
}

控制器:

 公众的ActionResult编辑(字符串ID)
    {
        UserViewModel用户=新UserViewModel();
        AspNetUser aspNetUser =新AspNetUser();        如果(!string.IsNullOrEmpty(ID))
        {
            aspNetUser = db.AspNetUsers.Find(ID);
            user.Id = aspNetUser.Id;
            user.Email = aspNetUser.Email;
            user.AspNetRoles = aspNetUser.AspNetRoles;            变种角色=(从db.AspNetRolesř
                        选择R).ToList();
            user.Roles =角色;
        }        返回查看(用户);
    }

查看:

  @ Html.ListBox(AspNetRoles
    新MultiSelectList(Model.Roles,ID,姓名,
    Model.AspNetRoles.Select(M => m.Id)))


解决方案

您还没有发布你的模型,但现在看来,房地产 AspNetRoles 是一个复杂的对象的集合(您不能绑定到一个复杂的对象,只值类型 - 或者在列表框,值类型的集合的情况下)。您可以通过更改 AspNetRoles INT [] (假设 ID 的属性角色 INT

  @ Html.ListBoxFor(M = GT; m.AspNetRoles,新的SelectList(Model.Roles,ID,姓名))

请注意,它总是最好使用强类型的辅助。

I have a ListBox in a View displaying the possible choices. However, the user's role(s) are not shown as selected. In my UserViewModel the Roles collection contains the complete list of roles, and AspNetRoles collection contains only the roles to which the user belongs. I've tried multiple examples/variations but nothing ever shows as selected. I'm stuck.

ViewModel:

public class UserViewModel
{
    public string Id { get; set; }

    public string Email { get; set; }

    public ICollection<AspNetRole> Roles { get; set; }

    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
}

Controller:

    public ActionResult Edit(string id)
    {
        UserViewModel user = new UserViewModel();
        AspNetUser aspNetUser = new AspNetUser();

        if (!string.IsNullOrEmpty(id))
        {
            aspNetUser = db.AspNetUsers.Find(id);
            user.Id = aspNetUser.Id;
            user.Email = aspNetUser.Email;
            user.AspNetRoles = aspNetUser.AspNetRoles;

            var roles = (from r in db.AspNetRoles
                        select r).ToList();
            user.Roles = roles;
        }

        return View(user);
    }

View:

@Html.ListBox("AspNetRoles",
    new MultiSelectList(Model.Roles, "Id", "Name", 
    Model.AspNetRoles.Select(m => m.Id)))

解决方案

You have not posted your model, but it appears that property AspNetRoles is a collection of a complex object (you cannot bind to a complex object, only a value type - or in the case of ListBox, a collection of value type). You can handle this by changing AspNetRoles to int[] (assuming the ID property of Role is int)

@Html.ListBoxFor(m => m.AspNetRoles, new SelectList(Model.Roles, "Id", "Name"))

Note, Its always better to use the strongly typed helpers.

这篇关于MVC 5 MultiSelectList选定值不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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