I am working in mvc 4 application. I want to connect json data to a table in my application using jquery.I can convert the data set (for which I get data from the database) to json data using the method and get json data.But I don't know how to associate it with a table using jquery. Please tell me a way to solve this problem.
JSon data :
My json data is something like this.
[{"Location":"Chennai","Duration":"15","Sno":"1", "Date of Birth":"\/Date(-2209051800000)\/","Dateofjoin":"\/Date(-2209048200000)\/"}]
Jquery
$('#btnGoNew').click(function () { var url = '@Url.Content("~/Somecontroller/GetValue")'; $.getJSON(url, { id: valz }, function (data) {
View
<input type="button" class="MasterButton" id="btnGoNew"/> <table id="grd1"> <thead> <tr> <th>Location</th> <th>Duration</th> <th>Sno</th> <th>Date of Birth</th> <th>Dateofjoin</th> </tr> </thead> <tbody> <tr> <td></td> </tr> </tbody> </table>
controller
public JsonResult GetValue(string id) { JsonResult json = new JsonResult(); DataSet ds = LoadDoctordetailsNew(id); string returnData = GetJson(ds.Tables[0]); json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; json.Data = returnData; return json; } public static string GetJson(DataTable 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 = null; 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); }
source share