I have this model
public class DTO
{
public int Id {get;set;}
public string Name { get; set; }
public string LastName { get; set; }
public Dictionary<string, string> Items { get; set; }
}
Values in the Dictionary are coming from my database so they differ from one object to another. Anyway I need to return a Json in specific format in order to be understood by 3rd party grid. Example code
public ActionResult Index()
{
DTO dto = new DTO()
{
Id = 1 ,
Name = "Employee1",
LastName = "last name value",
Items = new Dictionary<string, string>()
};
// properties .....
dto.Items.Add("Variable 1" , "Value 1 Goes here");
dto.Items.Add("Variable 2", "Value 2 Goes here");
dto.Items.Add("Variable 3", "Value 3 Goes here");
return Json(dto, JsonRequestBehavior.AllowGet);
}
the desired Json should be like this
{"Id":1, "Name":"Employee1","LastName":"Last Name Value","Variable 1":"Value 1 Goes here","Variable 2":"Value 2 Goes here","Variable 3":"Value 3 Goes here"}
Notice that Dictionary representation MUST not be an array i.e Converting rows to cols. I've tried a lot using JsonWriter and converters but I could not achieve this result.