简体   繁体   中英

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);
}

将可枚举直接传递给Json方法,而不是将其包装在匿名对象中。

return Json(statesHighlight, JsonRequestBehavior.AllowGet);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM