JavaScript运行时错误:无法获取未定义或空引用的属性“0” [英] JavaScript runtime error: Unable to get property '0' of undefined or null reference

查看:85
本文介绍了JavaScript运行时错误:无法获取未定义或空引用的属性“0”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临错误 -

JavaScript运行时错误:无法获取未定义或空引用的属性'0'



我已经尝试使用IE 10和Chrome,并且在执行操作后面临错误。



这段代码如下所示。



错误可能很小但肯定是我错过了。 :(



有人可以帮忙。



感谢您的帮助。



---查看

I'm facing the error -
JavaScript runtime error: Unable to get property '0' of undefined or null reference

I've tried it with IE 10 and Chrome and is facing the error after action is executed.

The piece of code is given below.

The error may be a very small one but surely I'm missing it. :(

Can someone please help.

Thanks for your helps in advance.

--- View

@{
    ViewBag.Title = "ViewShelf";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>ViewShelf</h2>

<div>
    <table id="allBooks"></table>
    <div id="pager"></div>
</div>

<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-ui-1.10.0.js"></script>
<script src="~/Scripts/i18n/grid.locale-en.js"></script>
<script src="~/Scripts/jquery.jqGrid.min.js"></script>


<script type="text/javascript">
    $(function () {
        $("#allBooks").jqGrid({
            url: "/Library/ShowAllBooks",
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Id', 'Name', 'Author', 'ISBN', 'Copies'],
            colModel: [
                { key: true, hidden: true, name: 'Id', Index: 'Id' },
                { key: false, name: 'Name', Index: 'Name', editable: true },
                { key: false, name: 'Author', Index: 'Author', editable: true },
                { key: false, name: 'ISBN', Index: 'ISBN', editable: true },
                { key: false, name: 'Copies', Index: 'Copies', editable: true }],
            pager: jQuery('#pager'),
            rowNum: 5,
            rowList: [5, 10, 20],
            height:'100%',
            viewrecords: true,
            emptyrecords: 'No Books in Shelf',
            jsonReader: {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",
                repeatItems: false,
                id: "0"
            },
            autowidth: true,
            multiselect: false
        });
        jQuery("#allBooks").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false });
    });
</script>



---控制器


--- Controller

public JsonResult ShowAllBooks(string sidx, string sord, int page, int rows)
{
    LibraryModel model = new LibraryModel();

    JqGridResponse response = new JqGridResponse();
    int totalRecords = 0;
    int pageIndex = Convert.ToInt16(page) - 1;
    int pageSize = rows;
    string jsonDataString = string.Empty;

    var allBookResults = model.GetAllBooks().Select(t => new
    {
        t.Id,
        t.Name,
        t.Author,
        t.ISBN,
        t.Copies
    });

    totalRecords = allBookResults.Count(); // allBookResults.Count();
    var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);

    if (sord.ToUpper() == "DESC")
    {
        allBookResults = allBookResults.OrderByDescending(t => t.Name);
        allBookResults = allBookResults.Skip(pageIndex * pageSize).Take(pageSize);
    }
    else
    {
        allBookResults = allBookResults.OrderBy(t => t.Name);
        allBookResults = allBookResults.Skip(pageIndex * pageSize).Take(pageSize);
    }

    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,
        rows = allBookResults
    };

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

推荐答案

function (){


#allBooks)。jqGrid({
url: / Library / ShowAllBooks
datatype:' json'
mtype:' GET'
colNames:[' Id'' 名称'' 作者'' ISBN',< span class =code-string>' Copies'],
colModel:[
{key: true ,隐藏: true ,名称:' Id',索引:' Id'},
{key: false ,name:' Name',索引:' 名称',可编辑: true },
{key: false ,name:' 作者',索引:' 作者',editable: true },
{key: false ,名称:' ISBN',索引:' ISBN',可编辑: true },
{key: false ,name:' Copies',索引:' 复制',可编辑: true }],
pager:jQuery(' #pager'),
rowNum: 5
rowList:[ 5 10 20 ],
height:' 100%'
viewrecords: true
emptyrecords:' 货架上没有书籍
jsonReade r:{
root: rows
page: page
总计: total
记录: records
repeatItems: false
id: 0
},
autowidth: true
multiselect: false
});
jQuery( #allBooks)。jqGrid(' navGrid'' # pager',{edit: false ,添加: false ,del: false });
});
< / script >
("#allBooks").jqGrid({ url: "/Library/ShowAllBooks", datatype: 'json', mtype: 'GET', colNames: ['Id', 'Name', 'Author', 'ISBN', 'Copies'], colModel: [ { key: true, hidden: true, name: 'Id', Index: 'Id' }, { key: false, name: 'Name', Index: 'Name', editable: true }, { key: false, name: 'Author', Index: 'Author', editable: true }, { key: false, name: 'ISBN', Index: 'ISBN', editable: true }, { key: false, name: 'Copies', Index: 'Copies', editable: true }], pager: jQuery('#pager'), rowNum: 5, rowList: [5, 10, 20], height:'100%', viewrecords: true, emptyrecords: 'No Books in Shelf', jsonReader: { root: "rows", page: "page", total: "total", records: "records", repeatItems: false, id: "0" }, autowidth: true, multiselect: false }); jQuery("#allBooks").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false }); }); </script>



---控制器


--- Controller

public JsonResult ShowAllBooks(string sidx, string sord, int page, int rows)
{
    LibraryModel model = new LibraryModel();

    JqGridResponse response = new JqGridResponse();
    int totalRecords = 0;
    int pageIndex = Convert.ToInt16(page) - 1;
    int pageSize = rows;
    string jsonDataString = string.Empty;

    var allBookResults = model.GetAllBooks().Select(t => new
    {
        t.Id,
        t.Name,
        t.Author,
        t.ISBN,
        t.Copies
    });

    totalRecords = allBookResults.Count(); // allBookResults.Count();
    var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);

    if (sord.ToUpper() == "DESC")
    {
        allBookResults = allBookResults.OrderByDescending(t => t.Name);
        allBookResults = allBookResults.Skip(pageIndex * pageSize).Take(pageSize);
    }
    else
    {
        allBookResults = allBookResults.OrderBy(t => t.Name);
        allBookResults = allBookResults.Skip(pageIndex * pageSize).Take(pageSize);
    }

    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,
        rows = allBookResults
    };

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}


这篇关于JavaScript运行时错误:无法获取未定义或空引用的属性“0”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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