I want to create a table inside razor view.
<div class="row" style="margin-left:80%">
@if (ViewBag.IsGlobal == 1)
{
<script>
$(document).ready(function () {
$("#btnViewLocal").prop("disabled",true);
@foreach (var item in Model.GlobalNewsModel)
{
var gTable = "<tr class=\"tvl\">";
gTable += "<td>";
gTable += "<input id=\"testid\" value=\"testvalue\" class=\"form-control\" />";
gTable += "<td>";
gTable += "</tr>";
//gTable.append("#wholeNews tr:last").after(gTable); <-- I tried it like this it also not working.
}
$("#WholeNews tr:last").after(gTable); // I can't place thisone inside foreach loop,it's unrecognize the $
});
</script>
}
}
</div>
Then
I hardcoded that table without foreach loop and it's working.But my requirement is to put that one in inside foreach loop.for your reference i've put that one here.
var gTable = "<tr class=\"tvl\">";
gTable += "<td>";
gTable += '<input id="' + $(this).closest('tr').find('td :eq(0)').val() + "newsno" + '" disabled type="text\" class=\"form-control" value="' + $(this).closest('tr').find('td :eq(0)').val() + '">';
gTable += "</td>";
gTable += "</tr>";
$("#WholeNews tr:last").after(gTable);
@foreach()loop is razor code and is parsed on the server before the view is sent to the browser. Javascript does not exist at that point. Why are you not just creating the table using razor/html?