JQGrid 中未加载数据
Data is not loading in JQGrid
数据未加载到 jqgrid 中。当我 运行 代码 also.I 认为我在从控制器传递数据并在 view.When 中接收时遇到一些问题时没有错误 我将从数据库中检索到的数据加载到 [=25] =] table 它对我来说效果很好。请帮忙。
<script src="~/JQGridReq/jquery-1.9.0.min.js" type="text/javascript"></script>
<link href="~/JQGridReq/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
<script src="~/JQGridReq/jquery.jqGrid.js" type="text/javascript"></script>
<link href="~/JQGridReq/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
jQuery("#jQGridDemo").jqGrid({
url: '@Url.Action("Details1", "Details")',
datatype: "json",
colNames: ['Id', 'First Name', 'Last Name', 'Salary', 'Gender'],
colModel: [{ name: 'ID', index: 'ID', width: 20 },
{ name: 'FirstName', index: 'FirstName', width: 20 },
{ name: 'LastName', index: 'LastName', width: 20 },
{ name: 'Salary', index: 'Salary', width: 20 },
{ name: 'Gender', index: 'Gender', width: 20 }],
rowNum: 10,
mtype: 'GET',
rowList: [10, 20, 30],
pager: '#jQGridDemoPager',
viewrecords: true,
sortorder: 'desc'
});
</script>
model IEnumerable<MvcApplication31.Models.Employee>
@{
ViewBag.Title = "Details1";
}
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>List of All Employees</title>
</head>
<body bgcolor="red" align="center" style="font-family: Arial; font-size: 10pt">
<h2>List of Empolyees</h2>
@*<table style="border: solid 15px red; width: 100%; vertical-align: central;">
<tr>
<td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-weight: bold; font-size: 20pt; color: chocolate;">jQ Grid Example In ASP.NET C#
</td>
</tr>
<tr>
<td style="text-align: center; vertical-align: central; padding: 50px;">
<table id="dataGrid" style="text-align: center;"></table>
<div id="pagingGrid"></div>
</td>
</tr>
</table>
<div id="pagingGrid"></div>*@
@*<table style="border: solid 15px red; width: 100%; vertical-align: central;">
<tr>
<td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-weight: bold; font-size: 20pt; color: chocolate;">jQ Grid Example In ASP.NET C#
</td>
</tr>
<tr>
<td style="text-align: center; vertical-align: central; padding: 50px;">
<table id="dataGrid" style="text-align: center;"></table>
<div id="pagingGrid"></div>
</td>
</tr>
</table>*@
<!-- <table id="entrydata" border="1" height="200" width="400" align="center" bgcolor="pink">
<!-- <tr>
<th>
ID
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Salary
</th>
<th>
Gender
</th>
</tr>-->
@*@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem=>item.ID)
</td>
<td>
@Html.DisplayFor(modelItem=>item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem=>item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem=>item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem=>item.Salary)
</td>
</tr>
}
</table>-->*@
<table id="jQGridDemo">
</table>
<div id="jQGridDemoPager">
</div>
</body>
</html>
控制器代码:-
public ActionResult Details1()
{
string ConnectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
SqlDataReader rdr = null;
List<Employee> l = new List<Employee>();
SqlDataAdapter da;
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection("data source=.; database=Srivatsava; integrated security=SSPI"))
{
connection.Open();
da = new SqlDataAdapter("select * from Employee", connection);
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
l.Add(new Employee() { ID = int.Parse(dr[0].ToString()), FirstName = dr[1].ToString(), LastName = dr[2].ToString(), Salary = int.Parse(dr[3].ToString()),Gender=dr[4].ToString() });
}
//Create an instance of SqlCommand class, specifying the T-SQL command that
//we want to execute, and the connection object.
//SqlCommand cmd = new SqlCommand("Select Id,FirstName,LastName,Salary,Gender from tblEm", connection);
// rdr = cmd.ExecuteReader();
/* while (rdr.Read())
{
// get the results of each column
int id = (int)rdr["ID"];
string contact = (string)rdr["FirstName"];
string company = (string)rdr["LastName"];
int city = (int)rdr["Salary"];
string gender = (string)rdr["Gender"];
// print out the results
Console.Write(contact);
Console.Write(city);
Console.Write(company);
Console.WriteLine(gender);
Console.WriteLine(id);
}*/
connection.Close();
}
return View(l);
}
您将返回 View
而不是 Json
。您 return
声明应该喜欢以下内容。
return Json(l, JsonRequestBehavior.AllowGet);
数据未加载到 jqgrid 中。当我 运行 代码 also.I 认为我在从控制器传递数据并在 view.When 中接收时遇到一些问题时没有错误 我将从数据库中检索到的数据加载到 [=25] =] table 它对我来说效果很好。请帮忙。
<script src="~/JQGridReq/jquery-1.9.0.min.js" type="text/javascript"></script>
<link href="~/JQGridReq/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
<script src="~/JQGridReq/jquery.jqGrid.js" type="text/javascript"></script>
<link href="~/JQGridReq/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
jQuery("#jQGridDemo").jqGrid({
url: '@Url.Action("Details1", "Details")',
datatype: "json",
colNames: ['Id', 'First Name', 'Last Name', 'Salary', 'Gender'],
colModel: [{ name: 'ID', index: 'ID', width: 20 },
{ name: 'FirstName', index: 'FirstName', width: 20 },
{ name: 'LastName', index: 'LastName', width: 20 },
{ name: 'Salary', index: 'Salary', width: 20 },
{ name: 'Gender', index: 'Gender', width: 20 }],
rowNum: 10,
mtype: 'GET',
rowList: [10, 20, 30],
pager: '#jQGridDemoPager',
viewrecords: true,
sortorder: 'desc'
});
</script>
model IEnumerable<MvcApplication31.Models.Employee>
@{
ViewBag.Title = "Details1";
}
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>List of All Employees</title>
</head>
<body bgcolor="red" align="center" style="font-family: Arial; font-size: 10pt">
<h2>List of Empolyees</h2>
@*<table style="border: solid 15px red; width: 100%; vertical-align: central;">
<tr>
<td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-weight: bold; font-size: 20pt; color: chocolate;">jQ Grid Example In ASP.NET C#
</td>
</tr>
<tr>
<td style="text-align: center; vertical-align: central; padding: 50px;">
<table id="dataGrid" style="text-align: center;"></table>
<div id="pagingGrid"></div>
</td>
</tr>
</table>
<div id="pagingGrid"></div>*@
@*<table style="border: solid 15px red; width: 100%; vertical-align: central;">
<tr>
<td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-weight: bold; font-size: 20pt; color: chocolate;">jQ Grid Example In ASP.NET C#
</td>
</tr>
<tr>
<td style="text-align: center; vertical-align: central; padding: 50px;">
<table id="dataGrid" style="text-align: center;"></table>
<div id="pagingGrid"></div>
</td>
</tr>
</table>*@
<!-- <table id="entrydata" border="1" height="200" width="400" align="center" bgcolor="pink">
<!-- <tr>
<th>
ID
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Salary
</th>
<th>
Gender
</th>
</tr>-->
@*@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem=>item.ID)
</td>
<td>
@Html.DisplayFor(modelItem=>item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem=>item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem=>item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem=>item.Salary)
</td>
</tr>
}
</table>-->*@
<table id="jQGridDemo">
</table>
<div id="jQGridDemoPager">
</div>
</body>
</html>
控制器代码:-
public ActionResult Details1()
{
string ConnectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
SqlDataReader rdr = null;
List<Employee> l = new List<Employee>();
SqlDataAdapter da;
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection("data source=.; database=Srivatsava; integrated security=SSPI"))
{
connection.Open();
da = new SqlDataAdapter("select * from Employee", connection);
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
l.Add(new Employee() { ID = int.Parse(dr[0].ToString()), FirstName = dr[1].ToString(), LastName = dr[2].ToString(), Salary = int.Parse(dr[3].ToString()),Gender=dr[4].ToString() });
}
//Create an instance of SqlCommand class, specifying the T-SQL command that
//we want to execute, and the connection object.
//SqlCommand cmd = new SqlCommand("Select Id,FirstName,LastName,Salary,Gender from tblEm", connection);
// rdr = cmd.ExecuteReader();
/* while (rdr.Read())
{
// get the results of each column
int id = (int)rdr["ID"];
string contact = (string)rdr["FirstName"];
string company = (string)rdr["LastName"];
int city = (int)rdr["Salary"];
string gender = (string)rdr["Gender"];
// print out the results
Console.Write(contact);
Console.Write(city);
Console.Write(company);
Console.WriteLine(gender);
Console.WriteLine(id);
}*/
connection.Close();
}
return View(l);
}
您将返回 View
而不是 Json
。您 return
声明应该喜欢以下内容。
return Json(l, JsonRequestBehavior.AllowGet);