为什么我的jqgrid在asp.net mvc中不起作用? [英] Why my jqgrid not work in asp.net mvc?

查看:66
本文介绍了为什么我的jqgrid在asp.net mvc中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net mvc的新用户,并想让用户gridview为此目的使用JqGrid,请在视图页面中输入以下ajax代码:

I'm new in asp.net mvc and want to show user gridview for that purpose use JqGrid, in view page write this ajax code:

<script type="text/javascript">
        $(document).ready(function () {
            $("#tblJQGrid").jqGrid(
            {
                url: '/POS/GetTableData',
                datatype: "json",
                mtype: 'GET',
                colNames: ['First Name', 'Last Name'],
                colModel: [
                { name: 'EmloyeeId', index: 'EmloyeeId', width: 150, stype: 'text' },
                { name: 'FName', index: 'FName', width: 150 }
                ], rowNum: 10,
                sortname: 'EmloyeeId',
                viewrecords: true,
                sortorder: "desc",
                caption: "List Employee Details",
                scrollOffset: 0
            });
        });
    </script>


并在控制器中编写以下代码:


and in controller write this:

DataTable mytTable = new DataTable();
            mytTable.Columns.Add("First Name", typeof(string));
            mytTable.Columns.Add("Last Name", typeof(string));
            mytTable.Rows.Add("behzad", "razzaqi");
            return JsonConvert.SerializeObject(mytTable);


但是没有给我任何数据!,我该如何解决这个问题?谢谢.


But not show me any data!,How can i solve that problem?thanks all.

推荐答案

JqGrid不接受行中的任何json,它需要以与列模型中的name字段匹配的名称值对格式设置json.

JqGrid does not accept any json in rows it needs the json formatted in name value pairs which matches the name field in column model.

有关详细信息,请参见答案文档.

See the the answer for more details and the documentation for how the json data should look.

因此,您的解决方案将是更改网格代码,如下所示 添加LName列模型以保留姓氏

So the solution for you will be to change the grid code as follows add LName column model to hold the lastname

script type="text/javascript">
        $(document).ready(function () {
            $("#tblJQGrid").jqGrid(
            {
                url: '/POS/GetTableData',
                datatype: "json",
                mtype: 'GET',
                colNames: ['First Name', 'Last Name'],
                colModel: [
                { name: 'EmloyeeId', index: 'EmloyeeId', width: 150, stype: 'text' },
                { name: 'FName', index: 'FName', width: 150 },
                { name: 'LName', index: 'LName', width: 150 }
                ], rowNum: 10,
                sortname: 'EmloyeeId',
                viewrecords: true,
                sortorder: "desc",
                caption: "List Employee Details",
                scrollOffset: 0
            });
        });
    </script>

在您的控制器中,按如下所示更改代码

In your controller change your code as follows

       public JsonResult GetTableData()
    {
        var jsonData = new
        {
            total = 20,
            page = 1,
            records = 2,
            rows = new[] {

              new{
                  EmloyeeId = 1,
                  FName = "behzad",
                  LName = "razzaqi"
              },

               new {
                      EmloyeeId = 2,
                    FName = "robert",
                    LName = "john"
                }
               }

        };

        return Json(jsonData);
    }

这篇关于为什么我的jqgrid在asp.net mvc中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆