C# 如何使用Ado.net将sql red的表值转换为json并在jqgrid中显示

C# 如何使用Ado.net将sql red的表值转换为json并在jqgrid中显示,c#,jquery,.net,asp.net-mvc-4,jqgrid-asp.net,C#,Jquery,.net,Asp.net Mvc 4,Jqgrid Asp.net,我已经使用ado.net编写了一段代码,代码的主要目的是从sql表中检索数据并在html表中显示结果。我成功地完成了这项任务。我的问题是如何将红色值转换为jsondata并传递到视图以在JQGrid中显示?我已经在谷歌上搜索过了,但我只得到了实体框架相关的东西。请帮忙 提前谢谢 public ActionResult Details1() { string ConnectionString = ConfigurationManager.ConnectionStrings["Employe

我已经使用ado.net编写了一段代码,代码的主要目的是从sql表中检索数据并在html表中显示结果。我成功地完成了这项任务。我的问题是如何将红色值转换为jsondata并传递到视图以在JQGrid中显示?我已经在谷歌上搜索过了,但我只得到了实体框架相关的东西。请帮忙

提前谢谢

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);
}
要获取json,您应该返回json,如下所示

return Json(l, JsonRequestBehavior.AllowGet);
而不是

return View(l)

使用JsonRrsult更新操作返回类型,如下所示

public JsonResult Details1()
{
返回

return Json(l, JsonRequestBehavior.AllowGet);
在您的HTMLs更新中,Jqgrid用法如下所示,将url指向您的控制器/操作

$("#yourgrid").jqGrid({

          datatype: 'json',
          url: 'controller/action',

这应该行。

var jsonData=new{total=totalPages,page,records=totalRecords,rows=todoListResults},返回JsonjsonData,jsonrequestbehavior.AllowGet;我需要将我们传递的数据转换成这样的视图吗?@rao是的,您需要以jsgrid所期望的格式构造json。var jsonData=new{total=totalPages,page,records=totalRecords,rows=todoListResults},返回JsonjsonData,jsonrequestbehavior.AllowGet;我必须像这样格式化我们传递给视图的数据吗?如果你想要服务器端分页,那么你应该像这样格式化。