i have below method:
[HttpPost]
public ActionResult GetData()
{
var data= (dynamic)null;
using (DBContext context = new DBContext())
{
data= context.MyObject.Where(i=> i.TypeId == 1).OrderBy(k => k.Name).Select(w => new
{
description = w.Description
}).ToList();
}
return Json(data, JsonRequestBehavior.AllowGet);
}
so I want to convert the data correctly into a json object, but i am not sure if i am doing correctly. This data returned should be used in a javascript.
I have google a lot and i have found example like below, maybe I should do a similar thing, but i do not know how:
var keyValues = new Dictionary<string, string>
{
{ "emailSend", textBox1.Text },
{ "toEmail", textBox2.Text }
};
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(keyValues);
MessageBox.Show(json);
Newtonsoft.Json!Json? your callingJson()in your controller. This will convert your object into Json. No need for anything else.MessageBox.Showis a windows method and won't work in ASP.Net alsoreturn Json(data, JsonRequestBehavior.AllowGet);is the correct way