jQuery Datatable with MVC 5 and Entity Framework

后端 未结 2 2048
盖世英雄少女心
盖世英雄少女心 2021-01-30 19:06

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 exam

2条回答
  •  太阳男子
    2021-01-30 19:30

    Use EFDatatable

    What is EFDatatable?

    EFDatatable is a helper to create a grid with Jquery Datatable and provides an extension to retrive data generically from Entity Framework context. It possible to use many datatable.js features with Html helper. It gives serverside or client side options. There's more: Wiki Documentation

    @(Html.EF().Datatable()
            .Name("PersonGrid")
            .Columns(cols =>
            {
                cols.Field(a => a.Id).Visible(false);
                cols.Field(a => a.Name).Title("First Name").Class("text-danger");
                cols.Field(a => a.Age).Title("Age").Searchable(false);
                cols.Field(a => a.BirthDate).Title("Birth Date").Format("DD-MMM-Y");
                cols.Command(a => a.Id, "onClick", text: "Click").Title("");
            })
            .Filters(filter =>
            {
                filter.Add(a => a.Id).GreaterThanOrEqual(1);
            })
            .URL(Url.Action("GetDataResult"), "POST")
            .ServerSide(true)
            .Render()
    )
    

    With "ToDataResult(request)" extension function, data can get with server side pagination very simply.

    public JsonResult GetDataResult(DataRequest request)
        {
            DataResult result = context.People.ToDataResult(request);
            return Json(result);
        }
    

    Where can I get it?

    Install EFDatatable from the package manager console:

    PM> Install-Package EFDatatable
    

    Then add datatables.net Javascript and CSS files or links to your project.

    
    
    

提交回复
热议问题