0

I am working on ASP.net MVC Applciation

I am facing difficulty in preparing json response to return from a controller action method.

I need some thing like this:

{
    "cityMap": {"11": "Chennai", "12": "Mumbai", "13": "Delhi"},
    "rows": [
        { "SID": "1",  "SNAME": "ABC", "CITY": "11" },
        { "SID": "2",  "SNAME": "XYZ", "CITY": "12" },
        { "SID": "3",  "SNAME": "ACX", "CITY": "13" },
        { "SID": "4",  "SNAME": "KHG", "CITY": "13" },
        { "SID": "5",  "SNAME": "ADF", "CITY": "12" },
        { "SID": "6",  "SNAME": "KKR", "CITY": "11" }
    ]
}

I have cityMap values in the sortedlist names slLocations

I have row values in the form of list liStudents

I am using the following syntax to send the json response:

return JSON(new { rows=liStudents},jsonRequestBehaviour.AllowGet)

By using the above syntax , i am getting the json response like this:

 {
  "rows": [
            { "SID": "1",  "SNAME": "ABC", "CITY": "11" },
            { "SID": "2",  "SNAME": "XYZ", "CITY": "12" },
            { "SID": "3",  "SNAME": "ACX", "CITY": "13" },
            { "SID": "4",  "SNAME": "KHG", "CITY": "13" },
            { "SID": "5",  "SNAME": "ADF", "CITY": "12" },
            { "SID": "6",  "SNAME": "KKR", "CITY": "11" }
        ]
  }

Please help on how to extend json response to above by using liStudents and liLocations

1 Answer 1

1

Simply add the desired cityMap property to the anonymous object that you are passing to the view:

var model = new
{
    cityMap = slLocations,
    rows = liStudents,
}
return JSON(model, JsonRequestBehaviour.AllowGet);

This obviously assumes that the slLocations variable is a SortedList<string, string>.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Darin ..It worked beautifully..i am struggling on this from an hour..saved my time..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.