How To access and display the data from database using ajax jquery asp.net mvc
我正在尝试从数据库中获取数据并使用ajax和jquery在页面中显示它。 我是这个平台的新手,所以任何人都可以帮助我
模型:
1 2 3 4 5 6 7 8 9 10 11 | public class EmployeeModel { public int EmpId { get; set; } public string EmpName { get; set; } public int Age { get; set; } public int Salary { get; set; } } |
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | private static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString; public ActionResult GetUser() { return View(); } public JsonResult GetAllUser(int EmpId) { List<EmployeeModel> employee = new List<EmployeeModel>(); string query = string.Format("Select * From Employee", EmpId); SqlConnection connection = new SqlConnection(connectionString); { using (SqlCommand cmd = new SqlCommand(query, connection)) { connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { employee.Add( new EmployeeModel { EmpId = int.Parse(reader["EmpId"].ToString()), EmpName = reader.GetValue(0).ToString(), Age = int.Parse(reader["Age"].ToString()), Salary = int.Parse(reader["Salary"].ToString()) } ); } } return Json(employee, JsonRequestBehavior.AllowGet); } } |
AJAX:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | @{ ViewBag.Title ="Home Page"; var EmployeeModel = (List<second_day.Models.EmployeeModel>)Model; } <p id="getEmployee">Get Employee </p> <script src="~/Scripts/jquery-1.10.2.min.js"> <script type="text/javascript"> $(document).ready(function () { $('p#getEmployee').click(function () { GetEmployeeUsingAjax(); }); }); function GetEmployeeUsingAjax() { $.ajax({ type: 'GET', url: '@Url.Action("GetAllUser")', data:{"EmpId":EmpId}, dataType: 'json', success: function (data) { console.log(data); //$('#id').text(emp.employee.Id); //$('#firstName').text(emp.employee.FirstName); //$('#lastName').text(emp.employee.LastName); }, error: function (emp) { alert('error'); } }); } |
在这里,我需要获取数据,如果它不成功,其他成功通过错误
我是这个平台的新手,任何人都可以帮助我
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | function GetEmployeeUsingAjax() { var EmpId = 2; $.ajax({ type: 'GET', url: '@Url.Action("GetAllUser")', data: {"EmpId": EmpId }, dataType: 'json', success: function (data) { alert(data); //$('#id').text(emp.employee.Id); //$('#firstName').text(emp.employee.FirstName); //$('#lastName').text(emp.employee.LastName); }, error: function (emp) { alert('error'); } }); } [HttpGet] public JsonResult GetAllUser(int EmpId) { // your code } plus string.Format("Select * From Employee where empid = {0}",EmpId) |
请检查以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var var EmpId = 2; var abc = { "EmpId": EmpId }; $.ajax( { url: '/ControllerName/GetAllUser/', type:"GET", async: false, dataType:"json", contentType:"application/json;", data: JSON.stringify(abc), success: function (result) { alert(data); } }); |
行动应该是:
1 2 3 4 5 | [HttpGet] public JsonResult GetAllUser(int EmpId) { } |
快乐!!