javascript - 在 ASP.Net MVC Razor 中使用 jquery 附加 C# 字符串

标签 javascript c# jquery asp.net-mvc razor

我正在使用 ASP.Net MVC Razor。我已尝试以下方法在我的表主体中添加不起作用的表行:

<script>
            $(document).ready(function() {

            @foreach (var billdetail in Model.BillDetails)
            {
                var row = "<tr class='productTableRow'><td>" + billdetail.ModelName + "</td><td class='price' >" + billdetail.Price + " </td><td><input type='text' class='form-control unit' data-validation='Unit' nonZero='true' currentStockAmount=" + billdetail.CurrentAmount + " /></td><td class='total'>"+(billdetail.Price*billdetail.Price)+"</td><td><input type='button' class='btn btn-danger removeRow' data-modelId=" + billdetail.ModelId + " value='Remove'  / ></td><td style='visibility:hidden;'>" +
                                   "<input type='hidden' class='hiddenModel' value='" + billdetail.ModelId + "'>" +
                                   "<input type='hidden' class='hiddenPrice' value='" + billdetail.Price + "'>" +
                                   "</td></tr>";

                @:$("#productBody").append(@row);
            }

        });
    </script>

它显示错误:未捕获的语法错误:意外的标记& 正确的做法是什么。

最佳答案

我不明白你为什么要生成 javascript,它将在加载时附加此内容。为什么不在 View 中简单地使用 Razor 生成表标记,如下所示:

<table id="productBody">
   @foreach (var billdetail in Model.BillDetails)
   {
   <tr class="productTableRow">
      <td>@(billdetail.ModelName)</td>
      <td class="price">@(billdetail.Price)</td>
      <td><input type="text" class="form-control unit" data-validation="Unit" nonZero="true" currentStockAmount="@(billdetail.CurrentAmount)" /></td>
      <td class="total">@(billdetail.Price*billdetail.Price)</td>
      <td><input type="button" class="btn btn-danger removeRow" data-modelId="@(billdetail.ModelId)" value="Remove" / ></td>
      <td style="visibility:hidden;"><input type="hidden" class="hiddenModel" value="@(billdetail.ModelId)"><input type="hidden" class="hiddenPrice" value="@(billdetail.Price)"></td>
   </tr>
   }
</table>

关于javascript - 在 ASP.Net MVC Razor 中使用 jquery 附加 C# 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31358353/

相关文章:

javascript - 从二进制 1's and 0' s 创建位图/ Canvas 图像以直观地查看 JavaScript 中的数据的算法?

javascript - 为 jison 生成的 css präprozessor 语言定义导入语句的语法

javascript - 同一 React 组件中的多个下拉菜单不起作用

c# - Entity Framework 5 性能不佳

c# - 使用 VS 2010 调试导入的 dll

javascript - 使用 Sizzle 选择缓存节点的子元素

javascript - jqgrid - beforeSearch和afterSearch函数不被触发

javascript - 让 <div> 同时消失

javascript - 在 HTML5 中使用 JavaScript 实现图像数组的拖放?

c# - 在 ASP.Net MVC 中保持干燥