简体   繁体   English

使用Linq将用ViewModel实例化的表作为列表生成“ SelectListItems”列表的正确方法是什么?

[英]What is the correct way to use Linq to reference the table you instantiate with your ViewModel as a list to generate a list of 'SelectListItems'?

I want to generate a strongly typed list of Roles in my controller along with two other lists which will be used in my view as various filters for sending an email. 我想在控制器中生成一个强类型的角色列表,以及另外两个列表,这些列表将在我的视图中用作发送电子邮件的各种过滤器。 I'm aware I can generate the Roles list using the following: 我知道我可以使用以下方法生成“角色”列表:

var viewModel = new ListViewModel
        {
            Roles = db.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Id.ToString(), Text = rr.Name }).ToList(),
        };

However as I am also wanting to pass two other lists to my controller I would prefer to attain the list of roles by writing the query in the following way: 但是,由于我也想将其他两个列表传递给我的控制器,因此我希望通过以下方式编写查询来获得角色列表:

var viewModel = db.Roles

           .Select(x => new SendGroupEmailViewModel
           {
               Roles = x.Select(rr => new SelectListItem { Value = rr.Id.ToString(), Text = rr.Name }).ToList(),

           };

That way I can then attain my other two lists as part of the same query, as opposed to the previous method where they would have to be effectively separate queries to the db. 这样,我就可以将其他两个列表作为同一查询的一部分,这与之前的方法不同,前一个方法必须将它们有效地分开到数据库中。 Obviously what is above doesn't work, I am unable .Select on the role table by writing Roles = x.Select. 显然以上内容不起作用,我无法通过写入Roles = x.Select在角色表上进行选择。

My question is, why am I unable to do this and what is the correct way to query the Roles table when writing it in a similar way to the above? 我的问题是,为什么我无法做到这一点?以与上述类似的方式编写角色表时,查询角色表的正确方法是什么?

Just to be clear, the reason it has to be var viewModel = db.Roles is because for whatever reason I can't access the ID/Name Roles list otherwise due to how Identity is setup in MVC. 明确地说,之所以必须为var viewModel = db.Roles是因为无论出于何种原因我都无法访问ID /名称角色列表,否则由于在MVC中如何设置身份。 I know there are other ways of attaining the list, I would just like to know how it can be done this specific way? 我知道还有其他方法可以获取该列表,我只想知道如何以这种特定方式完成此列表?

edit - I should add that Roles in my viewModel is as follows: 编辑-我应该在我的viewModel中添加Roles,如下所示:

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

controller 控制者

var oList = db.Roles.Select(x => new SendGroupEmailViewModel
               {
                   Roles = x.Roles.Select(rr => new SelectListItem { Value = rr.Id.ToString(), Text = rr.Name }).ToList();

    ViewData.myList = new SelectList(oList,

               }, 
                    "Value", "Text", 15); // 15 = selected item or you can omit this value

View 视图

<%=Html.DropDownList("myList") %>

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

相关问题 Linq-从IQueryable表达式的列表中排除用户-正确的方法是什么? - Linq - Excluding users from list in IQueryable expression - What is the correct way? 在viewmodel上生成一个列表 - Generate a List on viewmodel 在“所有者绘制的列表”框中使用控件的正确方法是什么? - What's the correct way to use controls in Owner drawn list box? 在List <[linq_custom_object]>()上实现.Distinct()的正确方法是什么? - What's the correct way to implement .Distinct() on a List<[linq_custom_object]>()? 收藏<T>对比列表<T>你应该在你的界面上使用什么? - Collection<T> versus List<T> what should you use on your interfaces? 如果您无法从 C# Windows 应用程序(使用 Linq)中使用自动生成的链接表,那么它的目的是什么? - What's the purpose of autogenerated linking table if you cannot use it from your C# Windows Application (using Linq)? 如何在.net核心中向ViewModel添加表列表 - How do you add a table list to a ViewModel in .net core 问题与 LINQ 和参考列表 - Issue With LINQ and Reference List 使用LINQ查询列表的最佳方法 - best way to use LINQ to query against a list 用实体更新列表的正确方法是什么? - What Is the Correct Way to Update a List with Entity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM