【问题标题】:Unterminated template literal in MVC razorMVC razor 中未终止的模板文字
【发布时间】:2019-02-19 01:19:47
【问题描述】:

我有以下动态填充数据表的代码:

<script>

        $(document).ready(function () {
            var table = $('#orders').DataTable();

            for (let item of @Model.ItemList) {

                table.row.add({
                    "id": item.id,
                    "product.name": item.product.name,
                    "quantity": item.quantity,
                    "linetaxes": item.linetaxes,
                    "discount": item.discount,
                    "lineitemtotal": item.lineitemtotal
                }).draw();
            }
        });

    </script>

当我运行它时,我收到以下错误消息:

未捕获的语法错误:未终止的模板文字

for(让项目的 System.Collections.Generic.List`1[Myproject.Models.LineItem])

谁能告诉我如何解决这个问题?锄头我可以在javascript中访问模型吗?

【问题讨论】:

    标签: javascript razor datatable


    【解决方案1】:

    您应该将@Html.RawJson.Encode 用于您的视图模型属性集合对象,其类型为List&lt;LineItem&gt;

    for (let item of @Html.Raw(Json.Encode(Model.ItemList)) {
    
                table.row.add({
                    "id": item.id,
                    "product.name": item.product.name,
                    "quantity": item.quantity,
                    "linetaxes": item.linetaxes,
                    "discount": item.discount,
                    "lineitemtotal": item.lineitemtotal
                }).draw();
    }
    

    通过在不使用Json.Encode 的情况下使用Model.ItemList,它会在页面渲染上隐式调用ToString(),从而返回对象的完全限定名称,即System.Collections.Generic.List&lt;Myproject.Models.LineItem&gt;

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 2014-08-08
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多