admin管理员组文章数量:1026629
I have established the connection to database and i want to display all the details in the db in the view in the table format but am unable to do it as my new can any one help.
public class EmployeeModel
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public int Age { get; set; }
public int Salary { get; set; }
}
Controller :
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);
}
}
View:
@{
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>
Can anyone suggest me solution
Data is fetched in console but not displaying in table format
I have established the connection to database and i want to display all the details in the db in the view in the table format but am unable to do it as my new can any one help.
public class EmployeeModel
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public int Age { get; set; }
public int Salary { get; set; }
}
Controller :
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);
}
}
View:
@{
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>
Can anyone suggest me solution
Data is fetched in console but not displaying in table format
Share Improve this question edited Dec 20, 2016 at 13:33 Matthew Wilcoxson 3,6221 gold badge45 silver badges48 bronze badges asked Dec 20, 2016 at 11:33 Sundar StalinSundar Stalin 992 gold badges2 silver badges9 bronze badges 7- Any error in console ? – 4b0 Commented Dec 20, 2016 at 11:34
- Success function Hitting or not? – Ghanshyam Singh Commented Dec 20, 2016 at 11:37
- it is hitting .I get error in console jquery-1.10.2.js:645 Uncaught TypeError: Cannot read property 'length' of undefined – Sundar Stalin Commented Dec 20, 2016 at 11:37
- Please check null values for json data – keerti Commented Dec 20, 2016 at 11:40
-
Show your controller method (the error is almost certainly because
data.data
is undefined - and I'm guessing its should be justdata
) – user3559349 Commented Dec 20, 2016 at 11:41
2 Answers
Reset to default 1Controller :-
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);
}
}
View:
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 id="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>
Your problem is this selector: $('#tblProducts tbody')
You have no table with that ID.
Changing it to $('.tblProducts tbody')
or renaming your table to <table id="tblProducts">
should do the trick.
As a suggestion, move the DOM manipulation outside the loop, it will have better performance:
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);
},
I have established the connection to database and i want to display all the details in the db in the view in the table format but am unable to do it as my new can any one help.
public class EmployeeModel
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public int Age { get; set; }
public int Salary { get; set; }
}
Controller :
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);
}
}
View:
@{
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>
Can anyone suggest me solution
Data is fetched in console but not displaying in table format
I have established the connection to database and i want to display all the details in the db in the view in the table format but am unable to do it as my new can any one help.
public class EmployeeModel
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public int Age { get; set; }
public int Salary { get; set; }
}
Controller :
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);
}
}
View:
@{
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>
Can anyone suggest me solution
Data is fetched in console but not displaying in table format
Share Improve this question edited Dec 20, 2016 at 13:33 Matthew Wilcoxson 3,6221 gold badge45 silver badges48 bronze badges asked Dec 20, 2016 at 11:33 Sundar StalinSundar Stalin 992 gold badges2 silver badges9 bronze badges 7- Any error in console ? – 4b0 Commented Dec 20, 2016 at 11:34
- Success function Hitting or not? – Ghanshyam Singh Commented Dec 20, 2016 at 11:37
- it is hitting .I get error in console jquery-1.10.2.js:645 Uncaught TypeError: Cannot read property 'length' of undefined – Sundar Stalin Commented Dec 20, 2016 at 11:37
- Please check null values for json data – keerti Commented Dec 20, 2016 at 11:40
-
Show your controller method (the error is almost certainly because
data.data
is undefined - and I'm guessing its should be justdata
) – user3559349 Commented Dec 20, 2016 at 11:41
2 Answers
Reset to default 1Controller :-
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);
}
}
View:
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 id="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>
Your problem is this selector: $('#tblProducts tbody')
You have no table with that ID.
Changing it to $('.tblProducts tbody')
or renaming your table to <table id="tblProducts">
should do the trick.
As a suggestion, move the DOM manipulation outside the loop, it will have better performance:
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);
},
本文标签:
版权声明:本文标题:javascript - Fetching the Data from db and displaying it in table format using Ajax, Jquery, Asp.Net MVC - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745121645a2135614.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论