2016-12-20 35 views
-1

我已經建立了與數據庫的連接,並且我希望在表格格式的視圖中顯示數據庫中的所有詳細信息,但是無法做到這一點,因爲我的新人可以幫助任何人。從數據庫中獲取數據並使用Ajax,Jquery,Asp.Net以表格格式顯示MVC

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; 
} 
<button>Click me</button> 

<script src="~/Scripts/jquery-1.10.2.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $(':button').click(function() { 
      GetEmployeeUsingAjax(); 
     }); 
    }); 

    function GetEmployeeUsingAjax() { 
     var EmpId = 2; 
     $.ajax({ 
      type: 'GET', 
      url: '@Url.Action("GetAllUser")', 
      data: { "EmpId": EmpId}, 
      dataType: 'json', 
      success: function (data) { 
       $.each(data, function (i, item) { 
        var rows = "<tr>" 
        + "<td>" + item.EmpID + "</td>" 
        + "<td>" + item.EmpName + "</td>" 
        + "<td>" + item.Age + "</td>" 
        + "<td>" + item.Salary + "</td>" 
        + "</tr>"; 
        $('#tblProducts tbody').append(rows); 
       }); 
      }, 
      error: function (emp) { 
       alert('error'); 
      } 
     }); 
    } 
</script> 
<table class="tblProducts"> 
    <thead> 
     <tr class="headings" style="background-color:#4495d1;"> 
      <th>EmpId</th> 
      <th>EmpName</th> 
      <th>Age</th> 
      <th>Salary</th> 
     </tr> 
</thead> 
    <tbody > 
    </tbody> 
</table> 

任何人都可以給我建議的解決方案

數據是在控制檯牽強,但不顯示在表格的形式

+0

任何錯誤安慰 ? –

+0

成功功能是否打? –

+0

它打。我在控制檯jQuery的1.10.2.js得到錯誤:645遺漏的類型錯誤:無法讀取的不確定 –

回答

0

控制器: -

private static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString; 
     public ActionResult GetUser() 
     { 
      return View(); 
     } 

     public JsonResult GetAllUser() 
     { 
      List<EmployeeModel> employee = new List<EmployeeModel>(); 
      string query = string.Format("Select * From Employee"); 
      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); 
      } 
     } 

查看:

function GetEmployeeUsingAjax() {   
     $.ajax({ 
      type: 'GET', 
      url: '@Url.Action("GetAllUser")', 
      data: { }, 
      dataType: 'json', 
      success: function (data) { 
    var rows; 
    $.each(data, function (i, item) { 
     rows += "<tr>" 
        + "<td>" + item.EmpID + "</td>" 
        + "<td>" + item.EmpName + "</td>" 
        + "<td>" + item.Age + "</td>" 
        + "<td>" + item.Salary + "</td>" 
      + "</tr>"; 
    }); 
    $('#tblProducts tbody').append(rows); 
}, 
      error: function (emp) { 
       alert('error'); 
      } 
     }); 
    } 
</script> 
<table class="tblProducts"> 
    <thead> 
     <tr class="headings" style="background-color:#4495d1;"> 
      <th>EmpId</th> 
      <th>EmpName</th> 
      <th>Age</th> 
      <th>Salary</th> 
     </tr> 
</thead> 
    <tbody > 
    </tbody> 
</table> 
+0

我可以知道你在控制器中的變化嗎 –

+0

我刪除了查詢和函數中的參數,就像你說的你想要所有員工的數據 –

+0

我不知道爲什麼一個答案沒有解釋什麼是錯的或什麼已經改變,從另一個答案代碼代碼,並設法仍然有錯誤被接受... @SundarStalin請注意,您將需要重命名您的表,如我的答案中的代碼工作 – rabelloo

1

你PR是這個選擇器:$('#tblProducts tbody')

你沒有該表的ID。

將其更改爲$('.tblProducts tbody')或將您的表格重命名爲<table id="tblProducts">應該有所斬斷。

被提出的建議,將DOM操作外循環,這將有更好的表現:

success: function (data) { 
    var rows; 
    $.each(data, function (i, item) { 
     rows += "<tr>" 
        + "<td>" + item.EmpID + "</td>" 
        + "<td>" + item.EmpName + "</td>" 
        + "<td>" + item.Age + "</td>" 
        + "<td>" + item.Salary + "</td>" 
      + "</tr>"; 
    }); 
    $('#tblProducts tbody').append(rows); 
}, 
+1

我需要顯示所有數據我指定EMPID = 2我只得到該id如何讓所有員工詳細 –

+0

如果你想爲什麼你逝去的EMPID = 2,如GetAllUser函數參數的所有員工的所有數據。你打開了我的眼睛是新 –

+1

@KeertiSystematixInfotech感謝試圖瞭解我自己我收到ID爲未定義時顯示 –

相關問題