【发布时间】:2014-05-20 12:43:27
【问题描述】:
我有 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 方法之上,但也没有用。
同样重要的是要注意我之前没有写过很多(任何)jQuery。
【问题讨论】:
-
@Url.Action("HomeController", "GetJsonData")' 是否正确?
-
我不确定。我无法让代码点击它。
-
这不会命中。因为arugment是错误的
标签: c# jquery json asp.net-mvc-4 jqgrid