带有 MVC 5 和实体框架的 jQuery 数据表 [英] jQuery Datatable with MVC 5 and Entity Framework

查看:18
本文介绍了带有 MVC 5 和实体框架的 jQuery 数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于在我的控制器中放置什么的指导,以便我可以对我的 jQuery 数据表使用服务器端处理.我正在使用 MVC 5 和实体框架.

I need some guidance on what to put in my controller so that I can use server-side processing with my jQuery datatables. I am using MVC 5 and Entity Framework.

位于:http://datatablesmvc.codeplex.com/documentation 的示例声明如下:

The example at: http://datatablesmvc.codeplex.com/documentation states the following:

public class HomeController : Controller {
[HttpPost]
 public ActionResult GetDataTables(DataTable dataTable) {
  List<List<string>> table = new List<List<string>>();
  //Do something with dataTable and fill table    
  return new DataTableResult(dataTable, table.Count, table.Count, table);
   }
 }

但是当我使用这样的 LINQ 时我该怎么办?

But what do I do when I am using LINQ such as this?

 public ActionResult Index()
         {
           var activity = db.Activity.Include(a => a.ActivityType);
              return View(activity.ToList());
          }

推荐答案

---------------------------------------- 更新答案-------------------------------

为什么要更新?

这个答案似乎一直受到 SO 用户的关注,我认为每个人都可以从一点"中受益.更新.

------------------------------- Updated answer -------------------------------

Why the update?

This answer seems to keep getting much attention from SO users and I thought everyone could benefit from a "little" update.

DataTables.Mvc 开始于一年多以前.它已经改变,现在被称为DataTables.AspNet.但这还不是全部.

DataTables.Mvc started over an year ago. It has changed and now it's called DataTables.AspNet. But that's not all.

当时的目标是帮助处理基类.问题是您只会得到一个 zip,并且应该手动将所有这些合并到您的项目中.此外,模型没有活页夹,集成也很无聊.

At that time, aim was to help with base classes. Problem is that you'd just get a zip and should manually merge all that into your project. Also, there was no binder for models and integration was really boring.

现在我们有一个带有 Nuget 包的模块化架构来提供帮助.您可以参考 Core 包并自己实现所有内容,也可以获取适当的包(Mvc5AspNet; WebApi2即将推出)带有本机模型绑定器、单行注册和完整的测试套件.

Now we have a modular architecture with Nuget packages to help. You can either reference Core package and implement everything yourself or you can get apropriate packages (Mvc5 or AspNet; WebApi2 is comming soon) with native model binders, one-line registration and a full test suite.

查看 dev 分支上的 samples 文件夹 (单击此处).不要忘记获取适当的 Nuget 包.您可以在此处找到它们的列表.

Check out samples folder on dev branch (click here). Don't forget to get appropriate Nuget packages. You can find a list of them here.

您可以将 DataTables 1.9、1.10 与旧 API 或 1.10 与新 API 一起使用.

You can either use DataTables 1.9, 1.10 with old API or 1.10 with the new API.

如果您选择新的 API(仅限 1.10),那么您会在这里和那里错过一些插件,但您可以使用 GitHub 上的 DataTables.AspNet 以帮助绑定.

If you choose the new API (1.10 only) than you'll miss some plugins here and there but you can use DataTables.AspNet on GitHub to help with the bindings.

如果没有,您可以查看并更改代码以匹配来自其他版本的请求变量(稍后将在我的项目中提供支持).

If not, you can take a look and change the code to match request variables from other versions (support will be provided later on my project).

重点是您必须处理三个项目:

Point is that you'll have to handle three items:

  1. 全局过滤/搜索
  2. 列过滤/搜索
  3. 列排序

给我一些代码!

如果您使用(或不使用)我的绑定类,这可能会从哪个版本发生变化.考虑一下你正在使用它,为了避免在这里处理请求参数,好吗?

Gimme some code!

That might change from which version and if you're using (or not) my binding class. Consider that you're using it, for the sake of avoiding handling request parameters here, ok?

所以,你可以像这样玩:

So, you can play with something like this:

[HttpPost]
public ActionResult Index([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestParameters)
{
    var totalCount = myDbContext.Set<Something>().Count();
    var filteredDataSet = myDbContext.Set<Something>().Where(_s => _s.ToLower().Contains(requestParameters.Search.Value));

    foreach(var column in requestParameters.Columns.GetFilteredColumns())
    {
        // Apply individual filters to each column.
        // You can try Dynamic Linq to help here or you can use if statements.

        // DynamicLinq will be slower but code will be cleaner.
    }

    var isSorted = false;
    IOrderedEnumerable<Something> ordered = null;
    foreach(var column in requestParameters.Columns.GetSortedColumns())
    {
        // If you choose to use Dynamic Linq, you can apply all sorting at once.
        // If not, you have to apply each sort manually, as follows.

        if (!isSorted)
        {
            // Apply first sort.
            if (column.SortDirection == Column.SortDirection.Ascendant)
                ordered.OrderBy(...);
            else
                ordered.OrderByDescending(...);

            isSorted = true;
        }
        else
        {
            if (column.SortDirection == Column.SortDirection.Ascendant)
                ordered.ThanBy(...);
            else
                ordered.ThanByDescending(...);
        }
    }

    var pagedData = ordered.Skip(requestParameters.Start).Take(requestParameters.Length);

    var dataTablesResult = new DataTablesResult(
        requestParameters.Draw,
        pagedData,
        filteredDataSet.Count(),
        totalCount
    );

    return View(dataTablesResult);
}

这篇关于带有 MVC 5 和实体框架的 jQuery 数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆