Skip to content

Can I return dataset to view through the json results ?

anand vemuri

anand vemuri

Asked Feb 11, 2015, 12:18 AM EST1 answer
Share
LinkedInX
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.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Pranay Rana
    0
    you can convert data table to json and can return as jsonresult...it will work


      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);
    });
    0