在View中生成时,SelectList不会选择项目

时间:2013-10-01 12:53:33

标签: asp.net-mvc razor selectlist

在View中创建SelectList时,我遇到了预选项目的问题。这是Razor代码:

@{
    ViewBag.Title = "RoleGroupMappings";
}

<h2>Map roles to groups:</h2>

<table>
    @foreach (var role in Model.Roles)
    {
        <tr>
            <td>
                <p>@role.RoleName: </p>
            </td>
            <td>
                @using (Html.BeginForm("MapGroupsToRole", "Role", FormMethod.Post))
                {
                    @Html.AntiForgeryToken()
                    @Html.Hidden("RoleId", role.RoleId)

                    @Html.DropDownList("Groups", new SelectList(role.Groups, "GroupId", "GroupName", role.Groups.Where(g => g.Mapped).Select(g => g.GroupId)), new { @class = "directory-groups", multiple = "multiple", style = "display: none;" })
                    <input type="button" value="Clear All" class="btn btn-danger clear-selection" />
                    <input type="submit" value="Save Changes" class="btn btn-success save-mappings" />
                }
            </td>
        </tr>
    }
</table>

有人知道这里有什么问题吗?

2 个答案:

答案 0 :(得分:1)

我认为您希望使用MultiSelectList代替普通SelectList。试试这个:

@Html.DropDownList("Groups", new MultiSelectList(role.Groups, "GroupId", "GroupName", role.Groups.Where(g => g.Mapped).Select(g => g.GroupId)), new { @class = "directory-groups", multiple = "multiple", style = "display: none;" })

更新

因为这对我来说很有用,所以我会告诉你我是如何设置这个测试项目的。希望这可以帮助您缩小问题的范围。测试模型设置:

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public List<Group> Groups { get; set; }
}

public class Group
{
    public Guid GroupId { get; set; }
    public string GroupName { get; set; }
    public bool Mapped { get; set; }
}

视图模型:

public class TestViewModel
{
    public List<Role> Roles { get; set; }
}


public ActionResult Index()
{
    var model = new TestViewModel
    {
        Roles = new List<Role>
        {
            new Role
            {
                RoleId = 1,
                RoleName = "Test",
                Groups = new List<Group>
                {
                    new Group
                    {
                        GroupId = new Guid("12345678-1234-1234-1234-123412341234"),
                        GroupName = "Group 1",
                        Mapped = false
                    },
                    new Group
                    {
                        GroupId = new Guid("12345678-5678-6789-1234-123412341234"),
                        GroupName = "Group 2",
                        Mapped = true
                    },
                    new Group
                    {
                        GroupId = new Guid("12345678-0000-6789-1234-123412341234"),
                        GroupName = "Group 3",
                        Mapped = true
                    }
                }
            }
        }
    };

    return View(model);
}

我的观点与您的观点相同,除了使用MultiSelect,我还删除了@class = "directory-groups"display: none;样式。第2组和第3组都将Mapped属性设置为true。这是加载视图的结果:

MultiSelectList

答案 1 :(得分:0)

我在控制器中发现了一个问题。我将ViewBag.Groups设置为某些值,显然它干扰了我的DropDownList,它具有相同的名称。对不起,我整天都在凝视,我找不到错误,所以我变得很绝望。我也应该看看我的控制器。