DiPix DiPix - 5 months ago 11
Javascript Question

How to pass LIST to view as JSON in MVC asp.net

I need to pass this list to my view:
enter image description here

But in this format:

{
"data": [
{
"FIRST_NAME": "Robert",
"LAST_NAME": "Meczybula",
"ID": 12421
...
},
...
]}

Answer

Basically you want to transform your list to the desired data contract. Linq is good for this:

var transform = new
    {
        data = data.Select(x => 
            new {
                x.FIRST_NAME,
                x.LAST_NAME,
                ...
            }),
        ...
    };

Then just serialize and return the transform object.