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.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pranay RanaPosted Feb 11, 2015, 7:22 AM
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
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary
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);
});