<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(function () { $("#btn1").click(function () { var id1 = parseInt($("#text1").val()); var url1 = "/Home/function1"; var data1 = { "id":id1 }; var json1 = JSON.stringify(data1); $("#jsondata").val(json1); $.post(url1, json1, function (data) { alert("success"); $("#id").val(data[0].Id); $("#col1").val(data[0].col1); $("#col2").val(data[0].col2); $("#col3").val(data[0].col3); },"json"); });
[HttpPost] public JsonResult function1(int id) { List<Table1> the1 = new List<Table1>(); string connStr = ConfigurationManager.ConnectionStrings["conndb"].ConnectionString; string cmdStr = "SELECT * FROM [Table1] WHERE [Id]=@Id;"; SqlDataReader dr = null; using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(cmdStr, conn)) { conn.Open(); cmd.Parameters.AddWithValue("@Id", id); using (dr = cmd.ExecuteReader()) { while (dr.Read()) { the1.Add(new Table1 { Id = Convert.ToInt32(dr["Id"]), col1 = dr["col1"].ToString(), col2 = dr["col2"].ToString(), col3 = dr["col3"].ToString() }); } } } } return Json(the1, JsonRequestBehavior.AllowGet); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)