1

我通过在 razor/cshtml 文件上调用 getJSON 来生成页面上 div 内容的内容。90% 的代码有效——从 2013 年到 1960 年——你可以在http://www.awardwinnersonly.com/从书籍下拉列表中选择“Hugos (Science Fiction)”看到,但在最后几个“记录” “我补充说(从 1959 年到 1946 年)显然导致对 getJSON('getHugos.cshtml') 的调用失败,因为页面根本不显示这些附加记录。

(问题“记录”暂时注释掉)

注意:带有“blankThis”和“blankThat”等时髦变量的“记录”以及类别设置为一年的“记录”都不是问题;它们只是相当笨拙-如果类别的长度为四个字符,则“记录”是一年,并且处理方式不同。

此外,以“--”为值的元素也不是问题 - 在这些情况下,不会创建相应的按钮(对于 Kindle、Hardcopy 或平装版)。

这是 cshtml 文件中的代码子集。省略号表示许多省略的“记录”;其中大部分,显然是问题所在,在第二组省略号和评论之后:

@{
    var books = new List<BookClass>
    {
        new BookClass{Year=2013, YearDisplay="blankYearDisplay", Category="2013", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",   
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
. . .
new BookClass{Year=2001, YearDisplay="2001", Category="Best Novella", Title="The Ultimate Earth", Author="Jack Williamson", KindleASIN="B00DV8TSHO", HardboundASIN="--", PaperbackASIN="1612421547",   
ImgSrc="http:images.amazon.com/images/P/B00DV8TSHO.01.MZZZZZZZ"}
. . . 
// the above works, from 2013 down to 1960, but something in the last few "records" is apparently causing it to fail
        new BookClass{Year=1959, YearDisplay="blankYearDisplay", Category="1959", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1959, YearDisplay="1959", Category="Best Novel", Title="A Case of Conscience", Author="James Blish", KindleASIN="--", HardboundASIN="B000J52BAI", PaperbackASIN="0345438353",     
ImgSrc="http:images.amazon.com/images/P/0345438353.01.MZZZZZZZ"},
        new BookClass{Year=1958, YearDisplay="blankYearDisplay", Category="1958", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1958, YearDisplay="1958", Category="Best Novel", Title="The Big Time", Author="Fritz Leiber", KindleASIN="B004UJHII4", HardboundASIN="0899685374", PaperbackASIN="B003YMNGGG",     
ImgSrc="http:images.amazon.com/images/P/B004UJHII4.01.MZZZZZZZ"},
        new BookClass{Year=1956, YearDisplay="blankYearDisplay", Category="1956", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1956, YearDisplay="1956", Category="Best Novel", Title="Double Star", Author="Robert A. Heinlein", KindleASIN="B0050OVMWG", HardboundASIN="0839824467", PaperbackASIN="0345330137",     
ImgSrc="http:images.amazon.com/images/P/B0050OVMWG.01.MZZZZZZZ"},
        new BookClass{Year=1955, YearDisplay="blankYearDisplay", Category="1955", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1955, YearDisplay="1955", Category="Best Novel", Title="They'd Rather Be Right (also known as The Forever Machine)", Author="Mark Clifton and Frank Riley", KindleASIN="--", HardboundASIN="--",     
PaperbackASIN="0881848425", ImgSrc="http:images.amazon.com/images/P/0881848425.01.MZZZZZZZ"},
        new BookClass{Year=1954, YearDisplay="blankYearDisplay", Category="1954", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1954, YearDisplay="1954", Category="Best Novella", Title="A Case of Conscience", Author="James Blish", KindleASIN="--", HardboundASIN="B000M0BM5A", PaperbackASIN="B005KEM8TW",     
ImgSrc="http:images.amazon.com/images/P/B005KEM8TW.01.MZZZZZZZ"},
        new BookClass{Year=1953, YearDisplay="blankYearDisplay", Category="1953", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1953, YearDisplay="1953", Category="Best Novel", Title="The Demolished Man", Author="Alfred Bester", KindleASIN="B00D2ITJLS", HardboundASIN="B000UF0KTQ", PaperbackASIN="0679767819",     
ImgSrc="http:images.amazon.com/images/P/B00D2ITJLS.01.MZZZZZZZ"},
        new BookClass{Year=1951, YearDisplay="blankYearDisplay", Category="1951", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1951, YearDisplay="1951", Category="Best Novella", Title="The Man Who Sold the Moon", Author="Robert A. Heinlein", KindleASIN="B00ELJZZ24", HardboundASIN="--", PaperbackASIN="1451639228",     
ImgSrc="http:images.amazon.com/images/P/B00ELJZZ24.01.MZZZZZZZ"},
        new BookClass{Year=1946, YearDisplay="blankYearDisplay", Category="1946", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1946, YearDisplay="1946", Category="Best Novella", Title="Animal Farm", Author="George Orwell", KindleASIN="B003ZX868W", HardboundASIN="0151010269", PaperbackASIN="184046254X",     
ImgSrc="http:images.amazon.com/images/P/B003ZX868W.01.MZZZZZZZ"}
    };
    Response.ContentType = "application/json";
    Json.Write(books, Response.Output);
}

当然,诸如“他们宁愿是正确的(也称为永远的机器)”之类的字符串不是问题,不是吗?显然,它编译并运行...

如果可能的话,我会尝试单步执行 getHugos.cshtml;另外,在浏览器控制台中查看是否有任何错误消息,但是是否有人知道与 cshtml 文件相关的 getJSON 的变幻莫测,这可以阐明这个难题?

更新

以这种方式重新排列和重新格式化:

var books = new List<BookClass> {
    new BookClass{Year=1959, YearDisplay="blankYearDisplay", Category="1959", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1958, YearDisplay="blankYearDisplay", Category="1958", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1956, YearDisplay="blankYearDisplay", Category="1956", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1955, YearDisplay="blankYearDisplay", Category="1955", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1954, YearDisplay="blankYearDisplay", Category="1954", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1953, YearDisplay="blankYearDisplay", Category="1953", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1951, YearDisplay="blankYearDisplay", Category="1951", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1946, YearDisplay="blankYearDisplay", Category="1946", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},

    new BookClass{Year=1959, YearDisplay="1959", Category="Best Novel",   Title="A Case of Conscience",                                       Author="James Blish",                  KindleASIN="--",         HardboundASIN="B000J52BAI", PaperbackASIN="0345438353", ImgSrc="http:images.amazon.com/images/P/0345438353.01.MZZZZZZZ"},
    new BookClass{Year=1958, YearDisplay="1958", Category="Best Novel",   Title="The Big Time",                                               Author="Fritz Leiber",                 KindleASIN="B004UJHII4", HardboundASIN="0899685374", PaperbackASIN="B003YMNGGG", ImgSrc="http:images.amazon.com/images/P/B004UJHII4.01.MZZZZZZZ"},
    new BookClass{Year=1956, YearDisplay="1956", Category="Best Novel",   Title="Double Star",                                                Author="Robert A. Heinlein",           KindleASIN="B0050OVMWG", HardboundASIN="0839824467", PaperbackASIN="0345330137", ImgSrc="http:images.amazon.com/images/P/B0050OVMWG.01.MZZZZZZZ"},
    new BookClass{Year=1955, YearDisplay="1955", Category="Best Novel",   Title="They'd Rather Be Right (also known as The Forever Machine)", Author="Mark Clifton and Frank Riley", KindleASIN="--",         HardboundASIN="--",         PaperbackASIN="0881848425", ImgSrc="http:images.amazon.com/images/P/0881848425.01.MZZZZZZZ"},
    new BookClass{Year=1954, YearDisplay="1954", Category="Best Novella", Title="A Case of Conscience",                                       Author="James Blish",                  KindleASIN="--",         HardboundASIN="B000M0BM5A", PaperbackASIN="B005KEM8TW", ImgSrc="http:images.amazon.com/images/P/B005KEM8TW.01.MZZZZZZZ"},
    new BookClass{Year=1953, YearDisplay="1953", Category="Best Novel",   Title="The Demolished Man",                                         Author="Alfred Bester",                KindleASIN="B00D2ITJLS", HardboundASIN="B000UF0KTQ", PaperbackASIN="0679767819", ImgSrc="http:images.amazon.com/images/P/B00D2ITJLS.01.MZZZZZZZ"},
    new BookClass{Year=1951, YearDisplay="1951", Category="Best Novella", Title="The Man Who Sold the Moon",                                  Author="Robert A. Heinlein",           KindleASIN="B00ELJZZ24", HardboundASIN="--",         PaperbackASIN="1451639228", ImgSrc="http:images.amazon.com/images/P/B00ELJZZ24.01.MZZZZZZZ"},
    new BookClass{Year=1946, YearDisplay="1946", Category="Best Novella", Title="Animal Farm",                                                Author="George Orwell",                KindleASIN="B003ZX868W", HardboundASIN="0151010269", PaperbackASIN="184046254X", ImgSrc="http:images.amazon.com/images/P/B003ZX868W.01.MZZZZZZZ"}
};

...表明这不是数据问题,除非以下内容由于某种原因确实存在问题:

Title="他们宁愿做对(也称为永远的机器)"

更新 2

不知何故,这最终出现在我的 getHugos.cshtml 文件的顶部 - 击败了我 - :

/P/

...所以第一行是:

/P/@{

...这就是它失败的原因。

它甚至可以编译对我来说很奇怪和/或令人毛骨悚然 - 事实上,如果它不会编译会很好,并指出我那条线!

但是感谢您对无与伦比的 alt 标签 JayC 的提示 - 它并没有导致 html 失败,但我确信它搞砸了我的 alts。

4

2 回答 2

1

在“终极地球”的结束 } 之后,您缺少一个逗号。

于 2013-09-19T19:34:59.787 回答
1

好的,现在很清楚了。我不确定你从哪里得到这个 htmlBuilder 位,但我很清楚,如果不是你的问题,这就是一个问题。让我们看看你的 Hugos html 生成器:

$.getJSON('GetHugos', function (data) {
    $.each(data, function (i, dataPoint) {
        if (IsClassYear(dataPoint.Category)) {
            htmlBuilder += '<div class=\"yearBanner\">' + dataPoint.Category + '</div>';
        } else {
            htmlBuilder += '<section class=\"wrapper\" ><a id=\"mainImage\" class=\"floatLeft\" href=\"' +
                Urlify(dataPoint.KindleASIN) + '\"' +
                ' target=\"_blank\"><img height=\"160\" width=\"107\" src=\"' +
                dataPoint.ImgSrc + '\ alt=\"' + dataPoint.Title + ' by ' + dataPoint.Author + ' book cover\ ></img></a>' +
                '<div id=\"prizeCategory\" class=\"categorySmallerFont\">' +
                dataPoint.Category +
                '</div><br/><cite id=\"prizeTitle\" >' +
                dataPoint.Title +
                '</cite><br/><div id=\"prizeArtist\" class=\"author\">' +
                dataPoint.Author +
                '</div><br/>';
            if (dataPoint.KindleASIN.trim().length > 2) {
                htmlBuilder += '<button><a href=\"' + Urlify(dataPoint.KindleASIN) + '\"' +
                    ' target=\"_blank\">Kindle</a></button>';
            }
            if (dataPoint.PaperbackASIN.trim().length > 2) {
                htmlBuilder += '<button><a href=\"' + Urlify(dataPoint.PaperbackASIN) + '\"' +
                    ' target=\"_blank\">Paperback</a></button>';
            }
            if (dataPoint.HardboundASIN.trim().length > 2) {
                htmlBuilder += '<button><a href=\"' + Urlify(dataPoint.HardboundASIN) + '\"' +
                    ' target=\"_blank\">Hardcover</a></button>';
            }
            htmlBuilder += '</section>';
        }//else
    }); //each

即使我们假设所有字段都是正确的 html 编码(通常,您不会假设),这也不会产生有效的 html。

线条

                ' target=\"_blank\"><img height=\"160\" width=\"107\" src=\"' +
                dataPoint.ImgSrc + '\ alt=\"' + dataPoint.Title + ' by ' + dataPoint.Author + ' book cover\ ></img></a>' +  

似乎缺少图像属性的引号srcalt. 此外,img不能有任何子节点,所以只需将其设为单个 tag <img />,虽然严格来说,这不会给您带来任何问题。

                ' target=\"_blank\"><img height=\"160\" width=\"107\" src=\"' +
                dataPoint.ImgSrc + '\" alt=\"' + dataPoint.Title + ' by ' + dataPoint.Author + ' book cover\" /></a>' +  

通常,我通过使用某种客户端模板解决方案来避免这种情况,就像您可能会在 underscore.js 中找到的那样,或者像 mustache、handlebars、knockout.js 或 Telerik 的剑道网格这样更花哨的东西。

于 2013-09-19T23:41:05.430 回答