【问题标题】:Data is not loading in JQGrid数据未在 JQGrid 中加载
【发布时间】:2016-03-15 16:07:36
【问题描述】:

数据未加载到 jqgrid 中。当我运行代码时也没有错误。我认为我在从控制器传递数据并在视图中接收时遇到了一些问题。当我在 html 表中加载从数据库检索到的数据时,它对我来说工作得很好。请帮忙。

<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);
        }

【问题讨论】:

    标签: jquery asp.net-mvc jqgrid-asp.net mvcjqgrid


    【解决方案1】:

    您返回的是View,而不是Json。您的return 声明应如下所示。

     return Json(l, JsonRequestBehavior.AllowGet);
    

    【讨论】:

    • 是的,它对我有用。非常感谢。但数据以原始格式显示。数据未以 jqgrid 格式加载。
    • 你应该在服务器端进行。此链接可能会对您有所帮助。 mikedormitorio.azurewebsites.net/BlogPost/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多