jotero jotero - 9 months ago 78
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.


Replace your return with:

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