MVC 4和JsonResult格式 [英] MVC 4 and JsonResult format

查看:158
本文介绍了MVC 4和JsonResult格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题,这个code的结果格式

have a problem with the result format of this code

public JsonResult getCategorias(int? id)
{
  var res = from c in db.Categorias
  where (( id.HasValue && c.CategoriaPadre == id.Value) || (!id.HasValue && c.CategoriaPadre == null))
  select new { id = c.Id, label = c.Descripcion };

  return this.Json(res, JsonRequestBehavior.AllowGet);
}

这回报 JSON

[{ID:21,标签:Marketing3},{ID:22,标签:Marketing4}]

但我需要一个 JSON 这种格式:

But i need a json with this format:

{21世纪:Marketing3,22:Marketing4}

我能做些什么?

非常感谢和抱歉,我的英语水平。

Thanks a lot and sorry my english.

推荐答案

替换你的回报王氏:

var dictionary = res.ToDictionary(v => v.id, v => label);
 return this.Json(dictionary, JsonRequestBehavior.AllowGet);

这篇关于MVC 4和JsonResult格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆