从ASP.NET MVC中的JSON数据中删除变量名称(Remove variable name from JSON data in ASP.NET MVC)

编程入门 行业动态 更新时间:2024-10-10 19:18:33
从ASP.NET MVC中的JSON数据中删除变量名称(Remove variable name from JSON data in ASP.NET MVC)

我正在尝试从一个JavaScript插件的数据库向客户端发送一串JSON,以突出显示它返回的状态。 但是,除了我需要的状态信息之外,我还得到了变量名。 无论如何,我可以更正格式,以便它看起来像这样:

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

我明白了:

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

这是我的控制器代码的样子:

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

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

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

return Json(statesHighlight, JsonRequestBehavior.AllowGet);

更多推荐

本文发布于:2023-04-29 01:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   名称   数据   MVC   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!