1

I'm not entirely sure how to define this question but basically I'm developing an ASP.Net application where I am generating a JsonResult called IndexJson.

My code is as follows:

public JsonResult IndexJson()
{
    var contacts = (from x in db.ContactSet
                   select new
                   {
                       x.AccountId,
                       x.FirstName,
                       x.LastName,
                       x.FullName,
                       x.JobTitle,
                       x.ParentCustomerId,
                       x.EMailAddress1,
                       x.Telephone1,
                       x.MobilePhone,
                       x.Fax,
                       x.GenderCode,
                       x.BirthDate
                   }).ToList();

    return Json(contacts, JsonRequestBehavior.AllowGet);
}

This works well at returning the following JSON:

[{/*contact info*/}, {/*contact info*/}, {/*contact info*/}, ...]

But now I want to return the following JSON (hard-coded for now, I will change the values later):

{
  "current": 1,
  "rowCount": 10,
  "rows": [{/*contact info*/}, {/*contact info*/}, {/*contact info*/}, ...],
  "total": 1123
}

How can I adapt my code to do that?

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143

4 Answers4

4

Simply wrap all into new anonymous object

return Json(new { current = 1, rowCount = 10, rows = contacts, total = 1123 }, 
            JsonRequestBehavior.AllowGet
           );
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Max
  • 6,821
  • 3
  • 43
  • 59
1

Looks like you're using a jQuery grid like jQuery Bootgrid at least by the look of the output you need.

If thats the case, you can do the following.

1 Create the Data Types you need

Input that comes to the controller

   public class RequestData
        {
            public int current { get; set; }
            public string rowCount { get; set; }
        /*Any other fields that come from the api*/
        }

The output you need

 public class ResponseData<T> where T : class
    {
        public int current { get; set; } // current page
        public int rowCount { get; set; } // rows per page
        public T rows { get; set; } // items
        public int total { get; set; } // total rows for whole query
    }

2 Put everything together

public JsonResult IndexJson(RequestData model)
{
    var contacts = (from x in db.ContactSet
    select new
    {
        x.AccountId,
        x.FirstName,
        x.LastName,
        x.FullName,
        x.JobTitle,
        x.ParentCustomerId,
        x.EMailAddress1,
        x.Telephone1,
        x.MobilePhone,
        x.Fax,
        x.GenderCode,
        x.BirthDate
    }).ToList();

    var tResult = 
        ResponseData<YourObjectType>()
        {
            current = model.current,
            rowCount = model.rowCount,
            rows = contacts,
            total = contacts.Count
         };

    return Json(tResult, JsonRequestBehavior.AllowGet);
}
bmvr
  • 817
  • 2
  • 9
  • 25
  • What should `YourObjectType` be? – Barry Michael Doyle Mar 29 '17 at 20:21
  • @BarryMichaelDoyle the same tipe of rows (contacts). E.g. `List contacts = new ... var tResult = ResponseData>() { current = model.current, rowCount = model.rowCount, rows = contacts, total = contacts.Count };` In your case it looks like it is a generic list of `List`. isn't it? – bmvr Mar 29 '17 at 21:09
  • I'm confused, I made a new question here: http://stackoverflow.com/questions/43103990/how-to-deal-with-jquery-bootgrid-request-data-in-mvc Which deals with this more thoroughly. – Barry Michael Doyle Mar 29 '17 at 21:15
0

You might like to define a (generic) type to store your extra fields and the list of items

public class ItemsWithTotal<T> // for pete sake find a better name!
{
    public int Current {get; set; }
    public int RowCount {get; set; }
    public int Total {get; set; }
    public List<T> Rows {get; set; }
}

Usage would be

var contacts = (from x in db.ContactSet
               select new
               {
                   x.AccountId,
                   x.FirstName,
                   x.LastName,
                   x.FullName,
                   x.JobTitle,
                   x.ParentCustomerId,
                   x.EMailAddress1,
                   x.Telephone1,
                   x.MobilePhone,
                   x.Fax,
                   x.GenderCode,
                   x.BirthDate
               }).ToList();
var result = new ItemsWithTotal<ContactSet>(){
     Current = 1,
     RowCount = 10,
     Total = 1123,
     Rows = contacts
}
return Json(result);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

You could create a viewmodel that looks the same. ie

    public MyReturnModel{
      public int Current {get; set;}
      public int rowCount {get; set;}
      public List<contacts> rows {get; set;}
      public int total {get; set;}
    }

Then in your method, just assign each property appropriately and return the JSONify the new viewmodel and return it.

utd1878
  • 95
  • 1
  • 9