droo46
droo46

Reputation: 114

Remove variable name from JSON data in ASP.NET MVC

I'm trying to send a string of JSON to the client from a database for a JavaScript plugin to highlight the states it gets back. However, in addition to the state information I need, I'm also getting the variable name. Is there anyway I can correct the formatting so that instead of it looking like this:

{"statesHighilght":["California","Washington","Utah","Utah","Florida","Kansas","Utah"]}

I get this:

["California","Washington","Utah","Utah","Florida","Kansas","Utah"]

This is what my controller code looks like:

public ActionResult highlight()
{
    var statesHighlight =
        db.Jobs
            .Select(r => r.State);
    return Json(new { statesHighilght }, JsonRequestBehavior.AllowGet);
}

Upvotes: 0

Views: 505

Answers (1)

Chris
Chris

Reputation: 4471

Pass the enumerable directly to the Json method instead of wrapping it in an anonymous object.

return Json(statesHighlight, JsonRequestBehavior.AllowGet);

Upvotes: 4

Related Questions