0

我的 _Layout.cshtml 像这样

 <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - E-Reader Statistics</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
       @* @Scripts.Render("~/bundles/jquery")*@
        <link href="~/jqGrid/css/ui.jqgrid.css" rel="stylesheet" />
        <script src="~/jqGrid/js/i18n/grid.locale-en.js"></script>
        <script src="~/jqGrid/js/jquery-1.9.0.min.js"></script>
        <script src="~/jqGrid/js/jquery.jqGrid.min.js"></script>
        <script src="~/jqGrid/js/jquery.jqGrid.src.js"></script>


    </head>

在 Index.chtml 中

@model List<LSStatisticalReport.ViewModels.GSViewModel>

@{
    ViewBag.Title = "Index";
}

@*<h2>General Statistics</h2>*@

<script type="text/javascript">
   // var jq = $.noConflict();
    $(document).ready(function () {
        var genStatGrid = $('#list');

        genStatGrid.jqGrid({
            caption: "General Statistics",
            url: '/Statistics/GetRecords/',
            datatype: "json",
            contentType: "application/json; charset-utf-8",
            mtype: 'POST',
            colNames: ['Student', 'Teacher', 'Date'],
            colModel: [
                            { name: 'studentDesignation' },
                            { name: 'teacherDesignation' },
                            { name: 'dateLength' },
              ],
            rowNum: 5
        });
    });
</script>

但是当我运行项目时,我在控制台中遇到错误:

“未捕获的类型错误:对象 [对象对象] 没有方法 'jqGrid'”

4

1 回答 1

0

这种类型的错误是这样说的:请在相关的初始化脚本之前定义 jqgrid 库。我以前见过这个错误,虽然你已经在 head 标签中定义了相关的库。因此,我通过从 head 标记中删除其库并将其放置在脚本之前的视图中并正常工作来解决了这个问题。

我认为这是由于页面的部分加载而发生的。所以在索引视图中使用它:

@model List<LSStatisticalReport.ViewModels.GSViewModel>

@{
    ViewBag.Title = "Index";
}

@*<h2>General Statistics</h2>*@
//added
<script src="~/jqGrid/js/jquery.jqGrid.min.js"></script>
<script src="~/jqGrid/js/jquery.jqGrid.src.js"></script>
//end added
<script type="text/javascript">
   // var jq = $.noConflict();
    $(document).ready(function () {
        var genStatGrid = $('#list');

        genStatGrid.jqGrid({
            caption: "General Statistics",
            url: '/Statistics/GetRecords/',
            datatype: "json",
            contentType: "application/json; charset-utf-8",
            mtype: 'POST',
            colNames: ['Student', 'Teacher', 'Date'],
            colModel: [
                            { name: 'studentDesignation' },
                            { name: 'teacherDesignation' },
                            { name: 'dateLength' },
              ],
            rowNum: 5
        });
    });
</script>
于 2013-08-20T08:26:45.390 回答