简体   繁体   English

MVC 强类型 IQueryable <igrouping<tkey, telement> > model </igrouping<tkey,>

[英]MVC Strongly typed IQueryable<IGrouping<TKey, TElement>> model

I'm trying to create a strongly typed model for one of my Views in MVC.我正在尝试为我在 MVC 中的一个视图创建强类型 model。 The model is the result of a LINQ GroupBy query so it is the type shown below (grouping employees by first letter of surname). model 是 LINQ GroupBy 查询的结果,因此它是如下所示的类型(按姓氏首字母对员工进行分组)。

@model IQueryable<IGrouping<string, Employee>>

I'm unsure why but it doesn't let me have a model of this type.我不确定为什么,但它不允许我拥有这种类型的 model。 The error message I get is: An opening "<" is missing the corresponding closing ">".我收到的错误消息是:开头的“<”缺少对应的结尾“>”。 Which is incorrect.这是不正确的。

I know I can create a view specific model and populate that instead but I'd like to know why this model doesn't seem to work?我知道我可以创建一个视图特定的 model 并填充它,但我想知道为什么这个 model 似乎不起作用?

By default, a very limited set of namespaces are available for direct use in razor views.默认情况下,可以在 razor 视图中直接使用一组非常有限的命名空间。 Try to expand it to fully qualified names and see if the problem persists:尝试将其扩展为完全限定名称并查看问题是否仍然存在:

@model System.Linq.IQueryable<System.Linq.IGrouping<string, Name.Space.Employee>>

I don't know why you'd be getting this error, since you appear to be using correct Razor code.我不知道你为什么会收到这个错误,因为你似乎使用了正确的 Razor 代码。 It's possible that there's actually a bug elsewhere in the page that is being made manifest through this incorrect error message.有可能页面的其他地方实际上存在错误,通过这条不正确的错误消息显示出来。

A workaround, which may help you determine the real source of the bug, would be to create your own strongly-typed model class, which could have this data as its property:一种可以帮助您确定错误真正来源的解决方法是创建您自己的强类型 model class,它可以将此数据作为其属性:

public class EmployeeListViewModel
{
    public IQueryable<IGrouping<string, Employee>> EmployeesByCompanyTitle {get;set;}
}

(There are those who would argue that this is a better approach anyway, since you can now add information to your view model more easily.) (有些人会争辩说这是一种更好的方法,因为您现在可以更轻松地向视图 model 添加信息。)

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

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