简体   繁体   English

Grid.Mvc +实体框架异常

[英]Grid.Mvc + Entity Framework exception

Need help with Grid.Mvc. 需要有关Grid.Mvc的帮助。 I'm passing IQueryable collection produced by Entity Framework and get exception 我正在传递由Entity Framework生成的IQueryable集合并获取异常

Action method: 动作方式:

public ActionResult Index(int id)
{
        IQueryable<Document> documents = _dbContext.Document;

        var model = new DocumentListViewModel()
        {                Documents = documents
        };

        return View(model);
}

DocumentListViewModel: DocumentListViewModel:

public class DocumentListViewModel
{
    public IQueryable<Document> Documents { get; set; }
}

View: 视图:

@model CompanyName.DocFlow.Web2.Models.DocumentListViewModel
@using GridMvc.Html;

@Html.Grid(Model.Documents).Columns(columns =>
                {
                    columns.Add(x => x.CreatedDate).Titled("Дата");//.Filterable(true);
                    columns.Add(x => x.IncomingNumber).Titled("Входящий номер");//.Filterable(true);
                    columns.Add(x => x.Description).Titled("Краткое описание");
                }).WithPaging(10)

Exception: 例外:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code EntityFramework.SqlServer.dll中发生类型为'System.NotSupportedException'的异常,但未在用户代码中处理

Additional information: The method 'Skip' is only supported for sorted input in LINQ to Entities. 附加信息:仅对于LINQ to Entities中的排序输入,才支持“跳过”方法。 The method 'OrderBy' must be called before the method 'Skip'. 必须在方法“跳过”之前调用方法“ OrderBy”。

The documentation states: 该文档指出:

If your data source is database (for example) you need to pass IQueryable collection to the grid. 例如,如果您的数据源是数据库,则需要将IQueryable集合传递给网格。 Grid.Mvc uses IQueryable interface to construct query expressions to your data collection. Grid.Mvc使用IQueryable接口为您的数据集合构造查询表达式。

When you go to some page the grid invokes .Skip(N).Take(N) methods and when you sort data the grid invokes OrderBy or OrderByDescending methods etc. 当您转到某个页面时,网格将调用.Skip(N).Take(N)方法;对数据进行排序时,网格将调用OrderBy或OrderByDescending方法等。

Tutorial link: Grid.Mvc 教程链接: Grid.Mvc

MVC Grid Description : MVC网格说明:

When you go to some page the grid invokes .Skip(N).Take(N) methods and when you sort data the grid invokes OrderBy or OrderByDescending methods etc. 当您转到某个页面时,网格将调用.Skip(N).Take(N)方法;对数据进行排序时,网格将调用OrderBy或OrderByDescending方法等。

You can do the sorting process encapsulate field get method or you can do this same operation in ActionResult method 您可以执行封装字段get方法的排序过程,也可以在ActionResult方法中执行相同的操作

Sample Code : 样例代码:

public class DocumentListViewModel
{
    private IQueryable<Document> _Documents;
    public  IQueryable<Document> Documents 
   { 
     get 
     { 
        return _Documents.Ordery(x=>x.Id);
     } 
     set 
     { 
        _Documnets = value;
     } 
   }
}

or 要么

public ActionResult Index(int id)
{
        IQueryable<Document> documents = _dbContext.Document.OrderBy(x=>x.Id);

        var model = new DocumentListViewModel()
        {             
            Documents = documents
        };

        return View(model);
}

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

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