How can I insert a JavaScript variable into razor? I'm incrementing counter by the amount of times the user clicks on insert-comment button, and then I have a JavaScript function that adds new input boxes for Comment. However, I'm having trouble properly inserting counter as the array index because I'm getting a "the name 'counter' does not exist in the current context" error.
Model:
public List<String> Comment {get; set;}
View:
var counter= 0;
jQuery(function($) {
$('#insert-comment').on("click", function(e) {
$(this).closest('tr').prev('tr').find('ul')
.append(
'<li>' +
'@Html.EditorFor(model => model.Comment[counter], new { htmlAttributes = new { @class = "form-control col-xs-12" } })' +
'</li>');
});
counter++;
});
Commentthat you manipulate.var comments = @Html.Raw(Json.Encode(model.Comment));