简体   繁体   中英

MVC Strongly typed IQueryable<IGrouping<TKey, TElement>> model

I'm trying to create a strongly typed model for one of my Views in MVC. 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 IQueryable<IGrouping<string, Employee>>

I'm unsure why but it doesn't let me have a model of this type. 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?

By default, a very limited set of namespaces are available for direct use in razor views. 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. 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:

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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