【发布时间】: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