.NET MVC 3 C# JSON (I think) problem
.NET MVC 3 C# JSON (I think) problem
I'm using .NET MVC 3 and C# with Entity Framework 4. All I want to do is having a simple straight forward ajax call to fill my table.
This is what I have in my View:
[code]
Firstname
Lastname
[/code]
And:
[code]$(document).ready(function () {
$('#tblData').dataTable({
"bProcessing": true,
"bServerSide": true,
"aoColumns": [
{ "sName": "Firstname" },
{ "sName": "Lastname" }],
"sAjaxSource": "/Administration/GetUsers",
});
});[/code]
And this is what I have in my Controller:
[code][HttpGet]
public JsonResult GetUsers()
{
MyEntities db = new MyEntities();
var users = from u in db.Users
select new
{
Firstname = u.Firstname,
Lastname = u.Lastname
};
var outJson = new
{
sEcho = "1",
iTotalRecords = users.Count(),
iTotalDisplayRecords = users.Count(),
aaData = users
};
return Json(outJson, JsonRequestBehavior.AllowGet);
}[/code]
When I try to execute it, I get this error:
"DataTables warning (table id = 'tblData'): Requested unknown parameter '0' from the data source for row 0".
Can someone explain to me what am I doing wrong?
Thank you!
This is what I have in my View:
[code]
Firstname
Lastname
[/code]
And:
[code]$(document).ready(function () {
$('#tblData').dataTable({
"bProcessing": true,
"bServerSide": true,
"aoColumns": [
{ "sName": "Firstname" },
{ "sName": "Lastname" }],
"sAjaxSource": "/Administration/GetUsers",
});
});[/code]
And this is what I have in my Controller:
[code][HttpGet]
public JsonResult GetUsers()
{
MyEntities db = new MyEntities();
var users = from u in db.Users
select new
{
Firstname = u.Firstname,
Lastname = u.Lastname
};
var outJson = new
{
sEcho = "1",
iTotalRecords = users.Count(),
iTotalDisplayRecords = users.Count(),
aaData = users
};
return Json(outJson, JsonRequestBehavior.AllowGet);
}[/code]
When I try to execute it, I get this error:
"DataTables warning (table id = 'tblData'): Requested unknown parameter '0' from the data source for row 0".
Can someone explain to me what am I doing wrong?
Thank you!
This discussion has been closed.
Replies
I've added this to my initialization code:
[code]"aoColumnDefs":
[
{ "mDataProp": "FirstName", "aTargets": [0] },
{ "mDataProp": "LastName", "aTargets": [1] },
],[/code]
Consider this resolved.