1

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:

<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.

4 Answers 4

2

I don't understand why you want to generate javascript which will append this on load. Why you don't simply generate table markup using Razor in your view, like this:

<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>
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

Due to the automatic html encoding in razor, you actually can change @:$("#productBody").append(@row); to the following to make it work.

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

Comments

0

It should be ok

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.