问题
I am using ASP.NET MVC 3. I am trying to populate my YUI data table with data from my database.
In my view I have the following code snippets:
@model IEnumerable<MyProject.News>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<link href="http://yui.yahooapis.com/2.8.2r1/build/reset-fonts-grids/reset-fonts-grids.css" rel="stylesheet" type="text/css">
<link href="http://yui.yahooapis.com/2.8.2r1/build/base/base-min.css" rel="stylesheet" type="text/css">
<link href="http://yui.yahooapis.com/2.8.2r1/build/button/assets/skins/sam/button.css" rel="stylesheet" type="text/css">
<link href="http://yui.yahooapis.com/2.8.2r1/build/datatable/assets/skins/sam/datatable.css" rel="stylesheet" type="text/css">
<link href="/Assets/Stylesheets/MyProject.css" rel="stylesheet" type="text/css">
<script src="http://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js" type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/element/element-min.js" type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/button/button-min.js" type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/datatable/datatable-min.js" type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/datasource/datasource-min.js" type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/connection/connection-min.js" type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/json/json-min.js" type="text/javascript"></script>
Here is how I create my data table:
<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function () {
var btnHelp, btnCreateNews;
btnHelp = new YAHOO.widget.Button('btnHelp');
btnCreateNews = new YAHOO.widget.Button('btnCreateNews');
btnCreateNews.addListener('click', function (e) {
window.location = '@Url.RouteUrl(Url.NewsCreate())';
});
var grdNewsColumnDefs = [
{ key: 'NewsId', label: 'Identifier' },
{ key: 'Title', label: 'Title' },
{ key: 'Body', label: 'Body' }
];
var grdNewsDataSource = YAHOO.util.DataSource('@Url.Action("JsonList")');
grdNewsDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
grdNewsDataSource.responseSchema = {
fields: [
{ key: 'NewsId' },
{ key: 'Title' },
{ key: 'Body' }
]
};
var grdNewsDataTable = new YAHOO.widget.DataTable('grdNews', grdNewsColumnDefs, grdNewsDataSource);
});
</script>
My News class:
public class News
{
public int NewsId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
My action method:
public ActionResult JsonList()
{
IEnumerable<News> newsList = newsService.FindAll();
return Json(newsList, JsonRequestBehavior.AllowGet);
}
The error that I get in the grid is:
Data error.
Not sure what is causing this?
回答1:
Try this:
Create a encapsulator class:
public class JsonEncapsulatorDto<T>
{
public IEnumerable<T> Data { get; set; }
public T DataSingle { get; set; }
}
in your action method use:
public ActionResult JsonList()
{
var data = new JsonEncapsulatorDto<News>();
data.Data = newsService.FindAll();
return Json(data , JsonRequestBehavior.AllowGet);
}
Now in your script:
var grdNewsDataSource = YAHOO.util.DataSource('@Url.Action("JsonList")');
grdNewsDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
grdNewsDataSource.responseSchema = {
resultsList: 'Data',
fields: [
{ key: 'NewsId' },
{ key: 'Title' },
{ key: 'Body' }
]
};
Hope it works.
来源:https://stackoverflow.com/questions/5066950/yui-datatable-in-asp-net-mvc-3