我正在使用带有剃刀视图引擎的ASP.NET MVC 3。
我的NewsController中有以下方法:
public JsonResult GetAllNews()
{
var items = newsService.FindAll();
var jsonResult = Json(items);
return jsonResult;
}
在我看来,我想尝试调用此方法来填充我的YUI数据表。我在这个方法的第一行放了一个断点但是没有命中断点。以下是调用此方法的视图中的代码:
var newsDataSource = YAHOO.util.DataSource('@Url.Action("GetAllNews");');
我甚至尝试过:
var newsDataSource = YAHOO.util.DataSource("/News/GetAllNews/");
两者似乎都不起作用。
这是我的数据表代码:
<div id="grdNews"></div>
<script type="text/javascript">
// News grid
var newsColumnDefs = [
{ key: "id", label: "Identifier" },
{ key: "title", label: "Title" },
{ key: "body", label: "Body" }
];
//var newsDataSource = YAHOO.util.DataSource('@Url.Action("GetAllNews");');
var newsDataSource = YAHOO.util.DataSource("/News/GetAllNews/");
newsDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
newsDataSource.responseSchema = {
fields: [
{ key: "id" },
{ key: "title" },
{ key: "body" }
]
};
var myDataTable = new YAHOO.widget.DataTable("grdNews", newsColumnDefs, newsDataSource);
</script>
我做错了什么?
答案 0 :(得分:1)
不要忘记让此方法也为GET请求返回JSON:
public JsonResult GetAllNews()
{
var items = newsService.FindAll();
return Json(items, JsonRequestBehavior.AllowGet);
}
同样设置datasource并不意味着它会调用该方法。也许你的代码中有一些其他部分存在问题。安装FireBug并查看是否发送了AJAX请求。
更新:
现在问题已经澄清,你在谈论YUI数据表,这里有一个完整的例子:
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult GetAllNews()
{
var news = new[]
{
new { id = 1, title = "title 1", body = "body 1" },
new { id = 2, title = "title 2", body = "body 2" },
new { id = 3, title = "title 3", body = "body 3" },
};
return Json(new
{
Result = news
}, JsonRequestBehavior.AllowGet);
}
}
查看(~/Views/Home/Index.cshtml
):
@{
ViewBag.Title = "Home Page";
}
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/datatable/assets/skins/sam/datatable.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/connection/connection-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/json/json-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/element/element-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/datasource/datasource-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/datatable/datatable-min.js"></script>
<script type="text/javascript">
var newsColumnDefs = [
{ key: "id", label: "Identifier" },
{ key: "title", label: "Title" },
{ key: "body", label: "Body" }
];
var newsDataSource = new YAHOO.util.DataSource('@Url.Action("GetAllNews")');
newsDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
newsDataSource.responseSchema = {
resultsList: 'Result',
fields: [ "id", "title", "body" ]
};
var myDataTable = new YAHOO.widget.DataTable("grdNews", newsColumnDefs, newsDataSource);
</script>
<div id="grdNews"></div>