简体   繁体   English

Kendo UI模板:无效的模板错误

[英]Kendo UI Template: Invalid Template Error

I'm trying to make a Kendo UI grid template. 我正在尝试制作Kendo UI网格模板。 When I run the following Chrome gives me the error message. 当我运行以下Chrome时,出现错误消息。 I've left out most of the rest of the message because it is just printing out all the generated HTML and javascript to the console. 我遗漏了其余大部分消息,因为它只是将所有生成的HTML和javascript打印到控制台。

Uncaught Error: Invalid template:'<div class="k-widget&#32;k-grid" id="l...

I'm trying to follow the "detailtemplate.cshtml" example on the page http://demos.kendoui.com/web/grid/detailtemplate.html 我正在尝试遵循http://demos.kendoui.c​​om/web/grid/detailtemplate.html页面上的“ detailtemplate.cshtml”示例

I'm having a hard time figuring out what I'm doing wrong. 我很难弄清楚自己在做什么错。 If I erase everything between the template's script tags and just put in some dumb HTML it all works fine so I'm sure the problem has something to do with the way I'm putting the kendo grid in. 如果我擦除模板的脚本标签之间的所有内容,并只放入一些哑的HTML,那么一切都可以正常工作,因此我确定问题出在我将剑道网格放入的方式上。

This is the code I have in my HTML page. 这是我在HTML页面中的代码。 The problem is somewhere inside the <script> with the id "GridDetailsTemplate". 问题出在<script>内部,其ID为“ GridDetailsTemplate”。

<div id="pendingApproval-tab">
    @(Html.Kendo().Grid<Moly.BusinessLogic.Entities.MolyAssayEntity>()
        .Name("pending-approval-grid")
        .HtmlAttributes(new { style = "overflow: auto; height: 600px"})
        .Columns(columns =>
        {
            columns.Bound(x => x.MolyLotID).HtmlAttributes(new {@class = "moly-lot-id"}).Hidden();
            columns.Template(@<div></div>).ClientTemplate("<input class='ready-checkbox' type='checkbox'/>").Title("Ready");
            columns.Bound(x => x.LotNo).Title("Lot").Groupable(false);
            columns.Bound(x => x.DateProduced).Format("{0:MM/dd/yy}").Title("Date");
            columns.Bound(x => x.NetWetWeight).Title("Net Wet Weight");
            columns.Bound(x => x.TareWeight).Title("Tare Weight");
            columns.Bound(x => x.NetDryWeight).Title("Dry Weight");
            columns.Bound(x => x.GrossWeight).Title("Gross Weight");
            columns.Bound(x => x.MolyWeight).Title("Lbs Mo");
            columns.Bound(x => x.MoisturePercent).Title("% H20");
            columns.Bound(x => x.MolyPercent).Title("Mo");
            columns.Bound(x => x.CopperPercent).Title("Cu");
            columns.Bound(x => x.LeadPercent).Title("Pb");
            columns.Bound(x => x.InsolublesPercent).Title("Insol");
            columns.Bound(x => x.ArsenicPercent).Title("As");
            columns.Bound(x => x.CalciumOxidePercent).Title("CaO");
            columns.Bound(x => x.IronPercent).Title("Fe");
            columns.Bound(x => x.MagnesiumOxidePercent).Title("MgO");
            columns.Bound(x => x.SodiumPercent).Title("Na");
            columns.Bound(x => x.BatchID).Title("Batch ID");
            columns.Bound(x => x.DunnageWt).Title("Dunnage Wt.");
            columns.Bound(x => x.Comment).Title("Comments");
        })
        .ToolBar(toolbar =>
        {
            toolbar.Save();
        })
        .Editable(editable => editable.Mode(GridEditMode.InCell))
        .ClientDetailTemplateId("GridDetailsTemplate")
        .DataSource(ds => ds
            .Ajax()
            .Batch(true)
            .Model(model => 
            {
                model.Id(m => m.MolyLotID);       
            })
            .Update(update => update.Action("UpdateMoly", "MolyLot"))
            .Read(read => read
                .Action("PendingApproval", "MolyLot")
                .Type(HttpVerbs.Post)
            )
        )
        .Events(events => events.DataBound("dataBound"))
    )
</div>

<script type="text/javascript">
    function dataBound() {
        this.expandRow(this.tbody.find("tr.k-master-row").first());
    }
</script>

<script id="GridDetailsTemplate" type="text/kendo-tmpl">
    @(Html.Kendo().Grid<Moly.BusinessLogic.Entities.UnroastedContainerEntity>()
        .Name("lot-details-grid")
        .Columns(columns =>
        {
            columns.Bound(x => x.ContainerNumber).Title("Number");
            columns.Bound(x => x.Type).Title("Type");
            columns.Bound(x => x.GrossWeight).Title("Gross Weight");
            columns.Bound(x => x.TareWeight).Title("Tare Weight");
        })
        .DataSource(DataSource => DataSource
            .Ajax()
            .Read(read => read.Action("GetBags", "MolyLot"))
        )
        .ToClientTemplate()
    )
</script>

Here is the "real" template that is generated with my template code: 这是用我的模板代码生成的“真实”模板:

<div class="k-widget&#32;k-grid" id="lot-details-grid">
    <table cellspacing="0">
        <colgroup>
            <col />
            <col />
            <col />
            <col />
        </colgroup>
        <thead class="k-grid-header">
            <tr>
                <th class="k-header" data-field="ContainerNumber" data-title="Number"
                scope="col"><span class="k-link">Number</span>
                </th>
                <th class="k-header" data-field="Type" data-title="Type" scope="col"><span class="k-link">Type</span>
                </th>
                <th class="k-header" data-field="GrossWeight" data-title="Gross&#32;Weight"
                scope="col"><span class="k-link">Gross Weight</span>
                </th>
                <th class="k-header" data-field="TareWeight" data-title="Tare&#32;Weight"
                scope="col"><span class="k-link">Tare Weight</span>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr class="t-no-data">
                <td colspan="4"></td>
            </tr>
        </tbody>
    </table>
</div>
<script>
    jQuery(function () {
        jQuery("\#lot-details-grid").kendoGrid({
            "columns": [{
                "title": "Number",
                "field": "ContainerNumber",
                "encoded": true
            }, {
                "title": "Type",
                "field": "Type",
                "encoded": true
            }, {
                "title": "Gross Weight",
                "field": "GrossWeight",
                "encoded": true
            }, {
                "title": "Tare Weight",
                "field": "TareWeight",
                "encoded": true
            }],
            "scrollable": false,
            "dataSource": {
                "transport": {
                    "read": {
                        "url": "/Moly.Web/controller/action"
                    }
                },
                "serverPaging": true,
                "serverSorting": true,
                "serverFiltering": true,
                "serverGrouping": true,
                "serverAggregates": true,
                "type": "aspnetmvc-ajax",
                "filter": [],
                "schema": {
                    "data": "Data",
                    "total": "Total",
                    "errors": "Errors",
                    "model": {
                        "fields": {
                            "UnroastedContainerID": {
                                "type": "number"
                            },
                            "MolyLotID": {
                                "type": "number"
                            },
                            "GrossWeight": {
                                "type": "number"
                            },
                            "Type": {
                                "type": "string"
                            },
                            "TareWeight": {
                                "type": "number"
                            },
                            "ContainerNumber": {
                                "type": "string"
                            },
                            "Units": {
                                "type": "string"
                            },
                            "MolyLot": {
                                "type": "object"
                            }
                        }
                    }
                }
            },
            "detailTemplate": kendo.template($('\#GridDetailsTemplate').html())
        });
    }); < \ / script >
        ' Generated code:'
    var o, e = kendo.htmlEncode;
    with(data) {
        o = '\n        <div class="k-widget&';
        32;
        k - grid " id="
        lot - details - grid "><table cellspacing="
        0 "><colgroup><col /><col /><col /><col /></colgroup><thead class="
        k - grid - header "><tr><th class="
        k - header " data-field="
        ContainerNumber " data-title="
        Number " scope="
        col "><span class="
        k - link ">Number</span></th><th class="
        k - header " data-field="
        Type " data-title="
        Type " scope="
        col "><span class="
        k - link ">Type</span></th><th class="
        k - header " data-field="
        GrossWeight " data-title="
        Gross & ;
        o += '32;Weight" scope="col"><span class="k-link">Gross Weight</span></th><th class="k-header" data-field="TareWeight" data-title="Tare&';
        32;
        Weight " scope="
        col "><span class="
        k - link ">Tare Weight</span></th></tr></thead><tbody><tr class="
        t - no - data "><td colspan="
        4 "></td></tr></tbody></table></div><script>
    jQuery(function(){jQuery("#lot - details - grid ").kendoGrid({"
        columns ":[{"
        title ":"
        Number ","
        field ":"
        ContainerNumber ","
        encoded ":true},{"
        title ":"
        Type ","
        field ":"
        Type ","
        encoded ":true},{"
        title ":"
        Gross Weight ","
        field ":"
        GrossWeight ","
        encoded ":true},{"
        title ":"
        Tare Weight ","
        field ":"
        TareWeight ","
        encoded ":true}],"
        scrollable ":false,"
        dataSource ":{"
        transport ":{"
        read ":{"
        url ":" / Moly.Web / controller / action "}},"
        serverPaging ":true,"
        serverSorting ":true,"
        serverFiltering ":true,"
        serverGrouping ":true,"
        serverAggregates ":true,"
        type ":"
        aspnetmvc - ajax ","
        filter ":[],"
        schema ":{"
        data ":"
        Data ","
        total ":"
        Total ","
        errors ":"
        Errors ","
        model ":{"
        fields ":{"
        UnroastedContainerID ":{"
        type ":"
        number "},"
        MolyLotID ":{"
        type ":"
        number "},"
        GrossWeight ":{"
        type ":"
        number "},"
        Type ":{"
        type ":"
        string "},"
        TareWeight ":{"
        type ":"
        number "},"
        ContainerNumber ":{"
        type ":"
        string "},"
        Units ":{"
        type ":"
        string "},"
        MolyLot ":{"
        type ":"
        object "}}}}},"
        detailTemplate ":kendo.template($('#GridDetailsTemplate').html())});});
<\/script>
    ;o+=;}return o;'

I came across this problem yesterday, so I found a solution that doesn't require you to remove the encoder. 昨天我遇到了这个问题,因此找到了不需要您删除编码器的解决方案。

using System.Web;
using System.Web.Mvc;
using System.Web.Security.AntiXss;
using System.Web.Util;

namespace Your.Namespace.Here
{
    public static class KendoMvcExtensions
    {

        public static IHtmlString ToMvcClientTemplate(this MvcHtmlString mvcString)
        {
            if (HttpEncoder.Current.GetType() == typeof (AntiXssEncoder))
            {
                var initial = mvcString.ToHtmlString();
                var corrected = initial.Replace("\\u0026", "&").Replace("%23", "#").Replace("%3D", "=").Replace("&#32;", " ");
                return new HtmlString(corrected);
            }

            return mvcString;
        }
    }
}

It checks to see if the AntiXssEncoder is active, and if it is, it removes the offending encoding characters. 它检查AntiXssEncoder是否处于活动状态,如果已激活,则将其删除有害的编码字符。 You can take out the encoder check if you want, but don't change the order that the characters are replaced... it is structured that way because .NET will make some of the characters (especially validator text) have mixed encoding (UTF-8 and Unicode in the same character set, example: \ , which un-encoded once becomes &#32 and un-encoded twice becomes " " ). 您可以根据需要进行编码器检查,但不要更改字符替换的顺序...它的结构是这样的,因为.NET将使某些字符(尤其是验证者文本)具有混合编码(UTF) -8和Unicode使用相同的字符集,例如: \ ,未经编码的一次为&#32 ,未经编码的两次为" "

To use it, just call .ToMvcClientTemplate() at the end of any offending control declaration. 要使用它,只需在任何有问题的控件声明的末尾调用.ToMvcClientTemplate() In the case of the OP, you would put it right after the closing parenthesis before you close the div. 对于OP,在关闭div之前将其放在右括号之后。

I'm keeping a copy of this code here in case someone somewhere else posts a better solution, as I've posted this code in several places. 我在这里保留了此代码的副本,以防有人在其他地方发布更好的解决方案,因为我已经在多个地方发布了此代码。

HTH HTH

For anyone having similar problem, i solved like this: 对于任何有类似问题的人,我都是这样解决的:

I have removed this line in the Web.config 我已经在Web.config中删除了这一行

<httpRuntime targetFramework="4.5" encoderType="System.Web.Security.AntiXss.AntiXssEncoder, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />.

I used a javascript function to clean my MVC partial before , I also added a remover for \\ because I fount the MVC version added many in. 我之前使用javascript函数清理MVC部分,还为\\添加了卸妆,因为我发现添加了许多MVC版本。

    function fixTemplate(template) {
    template = template.replace(/\\u0026/g, "&");
    template = template.replace(/%23/g, "#");
    template = template.replace(/%3D/g, "=");
    template = template.replace(/&#32/g, " ");
    template = template.replace(/\\/g, "");
    return template;
}

I don't know if there is a more efficient way to do this so feel free to comment. 我不知道是否有更有效的方法可以这样做,所以请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM