我正在使用 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/