一尘不染

如何使用ajax jquery asp.net mvc访问和显示数据库中的数据

ajax

我试图从数据库中获取数据,并使用ajax和jquery在页面中显示它。这个平台是新手,所以任何人都可以帮助我Model:

 public class EmployeeModel
 {
    public int EmpId { get; set; }

    public string EmpName { get; set; }

    public int Age { get; set; }

    public int Salary { get; set; }

 }

控制器:

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

阿贾克斯:

      @{
             ViewBag.Title = "Home Page";
             var EmployeeModel =                       (List<second_day.Models.EmployeeModel>)Model;
       }
     <div id="id"></div>
     <div id="firstName"></div>
<div id="lastName"></div>
<p id="getEmployee">Get Employee</p>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<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');
        }
    });
}

在这里,我需要在数据成功时获取数据,否则会通过错误获取数据

这个平台的新手可以帮助我吗


阅读 175

收藏
2020-07-26

共1个答案

一尘不染

 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)
2020-07-26