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

问题描述

我需要放什么在我的控制器,这样我可以使用服务器端处理与我的jQuery的DataTable一些指导。我使用MVC 5和Entity Framework。

在这个例子:<一href=\"http://datatablesmvc.$c$cplex.com/documentation\">http://datatablesmvc.$c$cplex.com/documentation规定如下:

 公共类HomeController的:控制器{
[HttpPost]
 公众的ActionResult GetDataTables(DataTable中的dataTable){
  清单&LT;名单,LT;串GT;&GT;表=新的List&LT;名单,LT;串GT;&GT;();
  //做一些与DataTable,并填写表格
  返回新DataTableResult(dataTable的,table.Count,table.Count,表);
   }
 }

不过,我该怎么办时,我使用LINQ像这样?

 公众的ActionResult指数()
         {
           变种活性= db.Activity.Include(一个= GT; a.ActivityType);
              返回查看(activity.ToList());
          }


解决方案

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

为什么更新?

这答案似乎不断得到太多关注,因此用户,我想每个人都可以从小的更新中受益。

什么是至今变化?

DataTables.Mvc 开始了一个去年同期水平。它已经改变,现在它被称为 DataTables.AspNet 。但是,这还不是全部。

当时,目的是要帮助基类。问题是,你只得到一个zip,应手动合并所有到您的项目。此外,还有是模型没有粘结剂和整合是很无聊。

现在我们有一个包的NuGet一个模块化的架构,以帮助。您可以参考核心包和自己实现的一切,或者你可以得到apropriate包( Mvc5 ASPNET ; WebApi2 与原始模型粘合剂,一种网上报名和一个完整的测试套件正在添加很快)

如何开始?

看看样品开发分支的文件夹(的)。
不要忘了得到妥善的NuGet包。你可以找到它们的列表。

-------------------------------原来的答复-------------- -----------------

首先第一件事情

您可以使用数据表1.9,1.10与旧的API或1.10与新的API。

如果您选择新的API(1.10只),比你在这里和那里错过了一些插件,但你可以使用,以帮助进行绑定。

如果没有,你可以看看,改变code匹配来自其他版本(支持稍后将我的项目提供)。

请求变量

实时交易

的一点是,你必须处理三个项目:


  1. 全局过滤器/搜索

  2. 列过滤器/搜索

  3. 列排序

给我一些code!

这可能会从哪个版本,如果你使用(或没有)我绑定类的变化和。想想看,你使用它,在这里避免处理请求参数的缘故,好不好?

所以,你可以像这样玩:

  [HttpPost]
公众的ActionResult指数([ModelBinder的(typeof运算(DataTablesBinder))] IDataTablesRequest requestParameters的)
{
    VAR TOTALCOUNT = myDbContext.Set&LT;东西方式&gt;()计数();
    变种filteredDataSet = myDbContext.Set&所述;东西&GT;()如(_s =方式&gt; _s.ToLower()包含(requestParameters.Search.Value));    的foreach(在requestParameters.Columns.GetFilteredColumns变种柱())
    {
        //应用单个过滤器到每一列。
        //你可以尝试动态LINQ到这里帮助,或者如果语句就可以使用。        // DynamicLinq会慢一些,但code会更干净。
    }    VAR isSorted = FALSE;
    IOrderedEnumerable&LT;&东西GT;责令= NULL;
    的foreach(在requestParameters.Columns.GetSortedColumns变种柱())
    {
        //如果您选择使用动态的LINQ,您可以一次应用所有排序。
        //如果没有,你必须手动应用每个排序,如下所示。        如果(!isSorted)
        {
            //应用第一个排序。
            如果(column.SortDirection == Column.SortDirection.Ascendant)
                ordered.OrderBy(...);
            其他
                ordered.OrderByDescending(...);            isSorted = TRUE;
        }
        其他
        {
            如果(column.SortDirection == Column.SortDirection.Ascendant)
                ordered.ThanBy(...);
            其他
                ordered.ThanByDescending(...);
        }
    }    VAR pagedData = ordered.Skip(requestParameters.Start)。取(requestParameters.Length);    VAR dataTablesResult =新DataTablesResult(
        requestParameters.Draw,
        pagedData,
        filteredDataSet.Count(),
        TOTALCOUNT
    );    返回查看(dataTablesResult);
}

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.

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);
   }
 }

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());
          }
解决方案

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

What's changed so far?

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

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.

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.

How to get started?

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.

------------------------------- Original answer -------------------------------

First things first

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

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

The real-deal

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

  1. Global filter/search
  2. Column filter/search
  3. Column sort

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);
}

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

07-23 13:13
查看更多