我知道这是一个愚蠢的问题,但是当我使用ado.net时,我无法弄清楚这个问题。我可以通过使用实体框架运行和使用jqgrid,但是当我使用ado.net时出现此错误。请帮忙。几乎是我的脑袋。我附上代码。提前谢谢。
$(function ()
{
$("#ListGrid").jqGrid
({
url: "/TodoList/GetTodoLists",
datatype: 'json',
mtype: 'Get',
colNames: ['Firstname', 'Gender', 'Id', 'LastName', 'Salary'],
colModel: [
{ key: false, name: 'Firstname', index: 'Firstname', editable: true },
{ key: false, name: 'Gender', index: 'Gender', editable: true },
{ key: true, hidden: true, name: 'Id', index: 'Id', editable: true },
{ key: false, name: 'LastName', index: 'LastName', editable: true },
{ key: false, name: 'Salary', index: 'Salary', editable: true }],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30, 40],
loadonce: true,
height: '100%',
viewrecords: true,
caption: 'Todo List',
emptyrecords: 'No records to display',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
Id: "0"
},
autowidth: true,
multiselect: false
});
});
@{
ViewBag.Title = "TodoList";
}
<h2>TodoList</h2>
<div>
<table id="ListGrid">
</table>
<div id="pager"></div>
</div>
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/jquery-ui-1.10.4.min.js"></script>
<script src="~/Scripts/i18n/grid.locale-en.js"></script>
<script src="~/Scripts/jquery.jqGrid.min.js"></script>
<script src="~/Scripts/TodoList.js"></script>
using MvcApplication32.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication32.Controllers
{
public class TodoListController : Controller
{
//
// GET: /TodoList/
public ActionResult Index()
{
return View();
}
public JsonResult GetTodoLists(string sidx, string sord, int page, int rows)
{
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();
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
var todoListsResults = l.Select(
a => new
{
a.ID,
a.FirstName,
a.LastName,
a.Salary,
a.Gender
});
int totalRecords = todoListsResults.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
if (sord.ToUpper() == "DESC")
{
todoListsResults = todoListsResults.OrderByDescending(s => s.FirstName);
todoListsResults = todoListsResults.Skip(pageIndex * pageSize).Take(pageSize);
}
else
{
todoListsResults = todoListsResults.OrderBy(s => s.FirstName);
todoListsResults = todoListsResults.Skip(pageIndex * pageSize).Take(pageSize);
}
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = todoListsResults
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
// return View(l);
}
}
}
答案 0 :(得分:0)
在这种情况下,有两个引用对_Layout.cshtml中的jquery库的其他引用,必须将其删除(*如果在cshtml文件中包含库标记),以便完全运行脚本,否则会得到相同的错误。< / p>