简体   繁体   English

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

[英]Append c# string using jquery in ASP.Net MVC Razor

I am working with ASP.Net MVC Razor.I have tried the following way to add a table row in my table body that is not working: 我正在使用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>

It is showing the error: Uncaught SyntaxError: Unexpected token & What is the proper way to do it. 它显示了错误: Uncaught SyntaxError: Unexpected token &正确的方法是什么。

You should just check the output of this on client side, from the source I would conclude that your code result in something like this on the client side: 您应该只在客户端上检查此输出,从源头上我会得出结论,您的代码在客户端上会产生如下所示:

//...
$("#productBody").append(<tr class='productTableRow'>...);
//...

No quotation marks, that I believe, is the main problem here. 我认为,这里没有引号是主要问题。

I don't understand why you want to generate javascript which will append this on load. 我不明白您为什么要生成将在加载时附加此内容的JavaScript。 Why you don't simply generate table markup using Razor in your view, like this: 为什么您不简单地在视图中使用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>

应该没关系

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

Due to the automatic html encoding in razor, you actually can change @:$("#productBody").append(@row); 由于剃须刀中的自动html编码,您实际上可以更改@:$("#productBody").append(@row); to the following to make it work. 以下内容以使其正常工作。

HtmlString s = new HtmlString(row);
@:$("#productBody").append("@s");

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

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