Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I need to load some data into a datatables table using server side processing.

I need to return a custom JSON object to the aaData property of the datatables object, to do some custom formatting. I can't simply return an array of strings.

I'm a newbie in datatables and javascript / ajax so please bare with me.

This is my controller action where I return an array (this is what I need to update):

 var result = from u in filteredResults
                     select new string[] {Convert.ToString(u.userId), u.userName, u.roleId.ToString(), u.type, u.isActive.ToString(), u.firstName, u.lastName, 
                     u.email, u.phone, u.Postcode, u.Street, u.Street, u.company, u.jobId.ToString(), u.job, u.country, u.countryName, u.City, u.LoginsNum.ToString(), 
                     u.LastLogin.ToString()};           

        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = result.Count(),
            iTotalDisplayRecords = result.Count(),
            aaData = result  ** NOT OK; must be updated **
        }, JsonRequestBehavior.AllowGet);

The resulting object must be in this form:

[
 { "prop1": "val1",
   "prop2": "val2",
    ......
   "propn": "valn"
 },
 .................


 { "prop1": "val1",
   "prop2": "val2",
    ......
   "propn": "valn"
 },

] 
share|improve this question

you have to make list as a JSON string and your action return type is JsonResult

public JsonResult GetData()
{
     var list = filteredResults.Select(temp => new[] { temp.userId.ToString(), temp.userName.ToString(), temp.email.ToString(), temp.phone.ToString(), temp.Postcode.ToString(), temp.Street.ToString(), temp.LastLogin.ToString() }).FirstOrDefault();
     return Json(list, JsonRequestBehavior.AllowGet);
}

you have to call GetData() action and that return JSON.

share|improve this answer

What I actually did was I used dynamic object syntax, to format my returned JSON array like this:

var result = from u in filteredResults
                     select new 
                     {
                         id = Convert.ToString(u.userId), 
                         userName = u.userName, 
                         roleId = u.roleId.ToString(), 
                         roleName = u.type, 
                         isActive =  u.isActive == true ? "Active" : "Inactive", 
                         firstName = u.firstName, 
                         lastName = u.lastName, 
                         email = u.email, 
                         phone = u.phone,
                         postcode = u.Postcode, 
                         street = u.Street, 
                         company = u.company, 
                         jobId = u.jobId.ToString(),
                         job = u.job, 
                         //countryKey = u.country, 
                         countryName = u.countryName, 
                         city = u.City, 
                         loginsNum = u.LoginsNum.ToString(), 
                         lastLogin = u.LastLogin.ToString()
                     };

        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = result.Count(),
            iTotalDisplayRecords = result.Count(),
            aaData = result
        }, JsonRequestBehavior.AllowGet);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.