0

我是 ASP.Net MVC 的新手。我正在尝试一个示例程序来将 JQGrid 与数据绑定。当我从 Controller 返回 JSON 数据时,系统提示我弹出“你要保存文件吗..”。我相信我的浏览器无法理解 JSON 数据。我浏览了 StackOverFlow 中的另一篇文章,但它们似乎都没有帮助我。我在设置为兼容模式的 IE8 上运行它。请看下面的代码:

索引.cshtml

@{
ViewBag.Title = "Home Page";
  }
  <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />  
   <link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet"    type="text/css" />  
   <link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />

  <script src="~/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>  
  <script src="~/Scripts/i18n/grid.locale-en.js" type="text/javascript"></script>  
  <script src="~/Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>

  <script type="text/javascript">
  $(document).ready(function () {
    $("#myGrid").jqGrid({
        url: '../../Home/About/GetJQGridData',
        datatype: 'json',
        contentType: 'application/json',
        myType: 'GET',
        colNames: ['Id', 'Name'],
        colModel: [
        { name: 'Id', index: 'Id' },
        { name: 'Name', index: 'Name' }
        ],
        jsonReader: {
            root: 'Data',
            id: 'id',
            repeatitems: false
        },
        pager: $('#myPager'),
        rowNum: 5,
        rowList: [2, 5, 10],
        width: 600,
        viewrecords: true,
        caption: 'Jqgrid MVC Tutorial'
        });
     });
   </script>



    <table id="myGrid"></table>  
     <div id="myPager"></div>`

我的控制器代码如下:

家庭控制器.cs

 [AcceptVerbs(HttpVerbs.Get)] 
    public ActionResult GetJQGridData()
    {
        var jqGridData = new JQGridObject()
        {
            Data = GetSomeSampleData(),
            Page = "1",
            PageSize = 3, // u can change this !  
            SortColumn = "Name",
            SortOrder = "asc"
        };

        return Json(jqGridData, JsonRequestBehavior.AllowGet);
    }

    public List<Fruit> GetSomeSampleData()
    {
        return new List<Fruit>  
        {  
        new Fruit{Id = 1, Name = "Apple" },  
        new Fruit{Id = 2, Name = "Melon" },  
        new Fruit{Id = 3, Name = "Orange" },  
        new Fruit{Id = 4, Name = "Grapes" },  
        new Fruit{Id = 5, Name = "Pineapple" },  
        new Fruit{Id = 6, Name = "Mango" },  
        new Fruit{Id = 7, Name = "Bannana" },  
        new Fruit{Id = 8, Name = "Cherry" }  
        };
    } 

任何帮助将不胜感激。谢谢

4

1 回答 1

1

让我感到震惊的第一件事是您的控制器/视图被错误地引用

url: '../../Home/About/GetJQGridData',

应该

url: '/Home/GetJQGridData',

您不需要在客户端上设置 jsonReader 。

于 2013-03-12T17:19:10.003 回答