[英]DataTable to JSON to Controller to jqGrid
我有3種形式, Associate.cs, HomeController.cs, Index.cshtml.
我從Associate.cs
加載DataTable
,並使用此代碼將其轉換為JSON格式。
public string GetAssociateFromDB()
//code omitted
var json = JsonConvert.SerializeObject(dt, Formatting.Indented);
jsonData = json;
}
}
return jsonData; //return is good
我從HomeController.cs
調用它。
public JsonResult GetJsonData()
{
Associate associate = new Associate();
string jsonData = associate.GetAssociateFromDB();
System.Diagnostics.Debug.WriteLine(jsonData); //for testing purposes
return Json(jsonData, JsonRequestBehavior.AllowGet); //need to pass to `Index.cshtml`
}
但是,除非將方法放在此代碼塊中,否則此代碼不會被點擊:
[HttpGet]
public ActionResult Index()
{
GetJsonData(); //if not called here, it never gets hit.
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
然后,我嘗試通過$.getJSON
方法將其傳遞給Index.cshtml,但從未實現。
jQuery(document).ready(function () {
var mydata;
$.getJSON('@Url.Action("HomeController", "GetJsonData")', function(data) {
datatype: 'json'
mydata = data;
console.log(data); //unsure if this works or not.
});
然后,我嘗試為jqGrid
提供該數據(這在上面的代碼下):
$("#grid").jqGrid({
data: mydata,
datatype: "json",
width: '100%',
colNames: ["Seq ID", "Fund ID", "Name", "Fund", "Bonus", "Allocation", "Blank", "Begin", "End"],
colModel: [{
name: 'seqid',
index: 'seqid',
editable: true,
},....//Code omitted
我似乎無法從Controller
獲取數據到Index.cshtml
。 我在博客上讀到,IE 8的cache
存在問題,這是我正在測試的問題,但是我敢肯定這不是我的最初問題。
我已經閱讀了jQGrid文檔並進行了多次搜索,但是唯一的例子就是針對PHP或其他上下文的這種事情。 我嘗試將[HttpGet]
放在GetJsonData
方法的上方, GetJsonData
也不起作用。
同樣重要的是要注意我以前沒有寫太多(任何)jQuery。
試試下面的代碼。
Url.Action('actionname','controllername')
JavaScript的:
$(document).ready(function () {
var mydata;
$.getJSON('@Url.Action("GetJsonData", "Home")', function(data) {
datatype: 'json'
mydata = data;
gridFn(data);
});
function gridFn(data)
{
//here grid code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.