jotero jotero - 1 year ago 137
JSON Question

MVC 4 and JsonResult format

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

this return a


But i need a
with this format:


What can i do?

Thanks a lot and sorry my english.

Answer Source

Replace your return with:

var dictionary = res.ToDictionary(v =>, v => label);
 return this.Json(dictionary, JsonRequestBehavior.AllowGet);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download