我有一个MVC应用程序,我需要从sql绘制表到Web.我无法预先创建表的模型,因此我在CS
HTML文件中使用
JavaScript代码动态创建DataTable:
var data,tableName = "#demotable",columns,str,jqxhr = $ .ajax({ data: { str: JSON.stringify("kurator") },url: MyAppUrlSettings.MyUsefulUrl,type: "GET",datatype: "json" }) .done(function() { data = JSON.parse(jqxhr.responseText); debugger; // Iterate each column and print table headers for Datatables $.each(data.columns,function(k,colObj) { str = "<th>" + colObj.data + "</th>"; $(str).appendTo(tableName + ">thead>tr"); debugger; }); // Add some Render transformations to Columns // Not a good practice to add any of this in API/ Json side data.columns[0].render = function(data,type,row) { return "<h4>" + data + "</h4>"; debugger; }; // Debug? console.log(data.columns[0]); $(tableName).dataTable({ data: data.data,columns: data.columns,fnInitComplete: function() { // Event handler to be fired when rendering is complete (Turn off Loading gif for example) console.log("Datatable rendering complete"); } }); debugger; });
我需要从ControllerAction返回一个JSON对象数组.这就是我创建DataTable的方法:
DataTable dt = new DataTable(); List<string> _columns = new List<string>() { "kurator","Filial","Klient","Saldo","Docs","no_Docs","Change","Status" }; for (int i = 0; i < _columns.Count; i++) { cols.Add(new Columns { ColumnID = "data",ColumnName = _columns[i] }); dt.Columns.Add(_columns[i],typeof(string)); } // Add rows to Table DaTarow _ravi; _ravi = dt.NewRow(); dt.Rows.Add(_ravi);
然后,最后,我需要将列和数据的数组放入JsonResult:
var data2 = debitor.Select(p => new { kurator = p.kurator,Filial = p.Filial,Klient = p.Klient,Saldo = p.Saldo,Docs = p.Docs,no_Docs = p.no_Docs,Change = p.Change,Status = p.Status }); // var data1 = JsonConvert.DeserializeObject(data); // var data = new JavaScriptSerializer().Serialize(dt,Formatting.Indented); // JObject RSS = new JObject(new JProperty("title","James Newton-King")); var columns = cols.Select(p => new { data = p.ColumnName }); return Json(new { data = data2,columns = columns },JsonRequestBehavior.AllowGet);
这工作正常=>它返回一个对象数组(当来自Model var data2时),但是当我尝试从我的DataTable(注释)中获取行数组时,Json中没有结果.
如何正确地将JsonResult放入我的DataTable行?我不能使用模型,因为我不知道SQL查询的结果中有多少列.
解决方法
从我的观点来看,问题有点复杂.
如果您使用的是Microsoft.AspNetCore.Mvc JsonResult类,它将根据输入参数生成以下结果:
与List<>一起使用时:
using Microsoft.AspNetCore.Mvc; class SomeClass { public string value1 {get; set;} public int value2 {get; set;} } // Controller code List<SomeClass> result = // Init list using Linq Select or similar return new JsonResult(result);
{ [ { value1: "some string",value2: 1 },{ value1: "another string",value2: 20 } ] }
与Dictionary<>一起使用时
class AnotherClass { public string value1 {get; set;} public int value2 {get; set;} } // Controller code // Dictionary should has key which became key in resulting object Dictionary<string,AnotherClass> result = // Init dictionary return new JsonResult(result);
这段代码像这样(一个大对象)生成JSON. ‘key1’和’key2’是来自Dictionary的键.
{ key1: { value1: "some string",value2: 1 },key2: { value1: "another string",value2: 20 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。