I want return dataset to view through the json results.
Is it possible? then please let me know! I am using MVC4
Thanks in advance.
Anand.
Can I return dataset to view through the json results ?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Pranay RanaRank #732 · 3.2K pointsPosted Feb 11, 2015, 2:22 AM EST
public string ConvertDataTabletoString()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true"))
{
using (SqlCommand cmd = new SqlCommand("select title=City,lat=latitude,lng=longitude,description from LocationDetails", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
http://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html
http://msdn.microsoft.com/en-us/library/vstudio/system.web.script.serialization.javascriptserializer.deserializeobject%28v=vs.110%29.aspx
after doing this
public ActionResult Action() {
return ConvertDataTabletoString();
}
in client script
$("#buttonid").click(function () {
var actionUrl = '@Url.Action("Action", "Controller")';
$.getJSON(actionUrl, displayData);
});