简体   繁体   English

实体框架显示从数据库到视图模型的项目列表

[英]Entity Framework displaying a list of items from database to viewmodels into view

I am using entity framework passing in a many to many relationship of "Role" into my controller. 我正在使用实体框架,将“角色”的多对多关系传递到控制器中。

In my controller, I have set: 在我的控制器中,我设置了:

    public ActionResult New()
    {
        var db = new MyContext();


        return View(new UsersNew
        {
            (from item in db.Roles
                     select item).Select(role => new RoleCheckbox
            {
                Id = role.Id,
                IsChecked = false,
                Name = role.Name
            }).ToList()
        });

My Viewmodel contains 2 classes: 我的Viewmodel包含2个类:

public class RoleCheckbox
{
    public int Id { get; set; }
    public bool IsChecked { get; set; }
    public string Name { get; set; }

}

public class UsersNew
{
    public IList<RoleCheckbox> Roles { get; set;}

    [Required, MaxLength(128)]
    public string Username { get; set; }

    [Required, DataType(DataType.Password)]
    public string Password { get; set; }

    [Required, MaxLength(128), DataType(DataType.EmailAddress)]
    public string Email { get; set; }

}

I have a strongly typed view from my UsersNew class in my ViewModel: 我在ViewModel中的UsersNew类中有一个强类型的视图:

        <ul class="list-group">
            @for (int i = 0; i < Model.Roles.Count; i++)
            {
                <li class="list-group-item">
                    @Html.Hidden("Roles[" + i + "].Id"), Model.Roles[i].Id)
                    <label for="Roles_@(i)__IsChecked">
                        @Html.CheckBox("Roles[" + i + "].IsChecked", Model.Roles[i].IsChecked)
                        @Model.Roles[i].Name
                    </label>
                </li>
            }
        </ul>

However, in my controller I have error stating: 但是,在我的控制器中我有错误说明:

"Error 1 Cannot initialize type 'myBlog.Areas.Admin.ViewModels.UsersNew' with a collection initializer because it does not implement 'System.Collections.IEnumerable " “错误1无法使用集合初始化程序初始化类型'myBlog.Areas.Admin.ViewModels.UsersNew',因为它没有实现'System.Collections.IEnumerable”

I have also tried this in my controller: 我也在控制器中尝试过此操作:

        using (var db = new MyContext())
            var Roles = MyBlog.Roles.Select(role => new RoleCheckbox
            {
                Id = role.Id,
                IsChecked = false,
                Name = role.Name
            }).ToList();
            return View(new UsersNew { Roles });

I could be doing this wrong, but how can I pass in my list of Roles from my database to my view, so that I can display a checkbox of role "Admin", "Moderator", and "User" on my user creation view? 我可能做错了,但是如何将角色列表从数据库传递到视图,以便可以在用户创建视图中显示角色“管理员”,“主持人”和“用户”的复选框?

Thank you 谢谢

The syntax is: 语法为:

new ClassName { PropertyName = value }

So we have: 因此,我们有:

return View(new UserNew {
    Roles = (from item in db.Roles
             select item).Select(role => new RoleCheckbox {
                 Id = role.Id,
                 IsChecked = false,
                 Name = role.Name
             }).ToList()
});

You need to declare your Roles as IEnumerable not IList 您需要将角色声明为IEnumerable而不是IList

public IEnumerable<RoleCheckbox> Roles { get; set;}

You can't iterate over IList . 您不能遍历IList See IList vs IEnumerable for Collections on Entities 有关实体集合,请参见IList与IEnumerable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM