【问题标题】:JQuery DataTables in ASP.NET MVC With A Query Source带有查询源的 ASP.NET MVC 中的 JQuery 数据表
【发布时间】:2015-02-06 18:30:18
【问题描述】:

总结
我正在尝试复制我目前在 ColdFusion 中拥有的类似工作技术,并在 MVC 5 中进行。我对 ASP.NET 非常陌生,所以此时我只有初步框架。我需要帮助来获取这些数据。 我目前在 ColdFusion 中所做的是在 CFC 中运行查询以填充 DataTable。 CFC 以 JSON 格式排列查询结果,我将其传递给 DataTable 的 AJAX 源。

这就是我在 ColdFusion 中可以做的事情。

Javascript

 $(document).ready(function() {
    var oTable = $('#table1').dataTable( {
    //The CFC source of the data
    "ajax": 'cfc/DataTablesSource.cfc?method=exampleFunction', 
    "columns": [                                            
        {"data": "ID", "width": "20%"},
        {"data": "FIRST_NAME", "width": "40%"},
        {"data": "LAST_NAME", "width": "40%"}
    ]
});

CFC(数据来源)

<cffunction name="exampleFunction" access="remote">

    <!---Query is run here--->
    <cfquery name="qSourceQuery">
        Select top 5 ID, FIRST_NAME, LAST_NAME
        From Name
    </cfquery>

    <!---The rest of this arranges the data in JSON format--->

    <cfset data = [] />

    <cfoutput query="qSourceQuery">
        <cfset obj = {
            "ID" = ID,
            "FIRST_NAME" = FIRST_NAME,
            "LAST_NAME" = LAST_NAME
        } />

        <cfset arrayAppend(data, obj) />
    </cfoutput>

    <cfprocessingdirective suppresswhitespace="Yes">
        <cfoutput>
            {"aaData":#serializeJSON(data)#}
        </cfoutput>
    </cfprocessingdirective>

    <cfsetting enablecfoutputonly="No" showdebugoutput="No">
</cffunction>

JSON 结果

{"aaData":[
    {"FIRST_NAME":"James","ID":"000001","LAST_NAME":"Smith"},
    {"FIRST_NAME":"David","ID":"000003","LAST_NAME":"Aaronson"},
    {"FIRST_NAME":"Jim","ID":"000005","LAST_NAME":"Thompson"},
    {"FIRST_NAME":"Alan","ID":"000006","LAST_NAME":"Abbott"},
    {"FIRST_NAME":"Lawrence","ID":"000012","LAST_NAME":"Abbott"}
]} 

HTML

<div id="tablediv">
    <div class="dataTables_wrapper">
        <table id="table1">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>
            </thead>
            <tbody>
                <!---Data and tags are dynamically generated--->
            </tbody>
        </table>
    </div>
</div>

MVC
从这一点开始,如果我自己提供 JSON 数据,我可以复制 DataTable 的创建(实际上有很多这样的例子。)但是,我希望表的源是一个查询。我在 MVC 中需要帮助。

Javascript

 $(document).ready(function() {
    var oTable = $('#table1').dataTable( {
    //New data source points to controller and action
    "ajax": '/TestPages/GetJSONMemberList', 
    "columns": [                                            
        {"data": "ID", "width": "20%"},
        {"data": "FIRST_NAME", "width": "40%"},
        {"data": "LAST_NAME", "width": "40%"}
    ]
});

查询结果应该是什么样的模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestApplication.Models
{
    public class MemberList
    {
        public string ID { get; set; }
        public string FIRST_NAME { get; set; }
        public string LAST_NAME { get; set; }
    }
}

控制器中的动作

public ActionResult getJSONMemberList()
    {
        return Json(new
        {
            aaData = new[]
                {
                    //Hard coded data here that I want to replace with query results
                    new MemberList { ID = "000001", FIRST_NAME = "James", LAST_NAME = "Smith" },
                    new MemberList { ID = "000003", FIRST_NAME = "David", LAST_NAME = "Aaronson" },
                    new MemberList { ID = "000005", FIRST_NAME = "Jim", LAST_NAME = "Thompson" }
                }
        }, JsonRequestBehavior.AllowGet);
    }

HTML

<div id="tablediv">
    <div class="dataTables_wrapper">
        <table id="table1">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>
            </thead>
            <tbody>
                <!---Data and tags are dynamically generated--->
            </tbody>
        </table>
    </div>
</div>

结论
因此,在我的新应用程序中,我的 Ajax 源指向一个控制器操作,该操作创建一个 JSON 格式的数据列表。我正在寻找有关将查询数据从 SQL Server 获取到该控制器或该模型的建议。我在此应用程序中使用实体框架,但我不必这样做。复制这个的最好方法是什么?这会涉及调用存储过程吗?如果是这样,我该怎么做?

【问题讨论】:

    标签: jquery asp.net ajax json asp.net-mvc


    【解决方案1】:

    使用 Entity Framework 和 LINQ,我能够做到这一点。

    在我的控制器中,我有这个

    //Used to connect to the Entities standing in for the database
    private NorthwindEntities db = new NorthwindEntities();
    

    此方法将运行一个查询,该查询将返回所需的 JSON 格式。

    public ActionResult getJSONQuery()
        {
                        //The table or entity I'm querying
            var query = db.Names
                .Where(p => p.ID > 1)
                .Select(p => new{p.ID, p.FIRST_NAME, p.LAST_NAME});
    
            return Json(new { aaData = query }, JsonRequestBehavior.AllowGet);
        }
    

    我的初始化看起来一样,只是指向运行查询的控制器。

    $(document).ready(function() {
        var oTable = $('#table1').dataTable( {
        //New data source points to controller and action
        "ajax": '/TestPages/GetJSONQuery', 
        "columns": [                                            
            {"data": "ID", "width": "20%"},
            {"data": "FIRST_NAME", "width": "40%"},
            {"data": "LAST_NAME", "width": "40%"}
        ]
    });
    

    【讨论】:

    • @madova,感谢您发布此消息。我正在使用 EF 和 DataTables 将 CF 应用程序迁移到 ASP.NET MVC,这是非常宝贵的。非常感谢!
    猜你喜欢
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多