I have problem with Razor syntax and jQuery, I’m trying to append some items to an empty table.
Here’s the code
@{
if (@ViewData["a"] != null)
{
var tab = @ViewData["a"] as List<string>;
foreach(var sp in @tab)
{
<text>
$('#files-table').append(@Html.Raw(@sp));
</text>
}
}
}
the @sp variable is like (I’ve formatted it here):
<tr>
<td>
<a class='some-class' my-type='abc' my-id='1'
target='_blank' href='/Home/a'>name123</a>
</td>
<td>
<label class='del' f-id='abc'>lorem</label>
</td>
</tr>
The table is still empty. I’ve tried many options, like .append('@Html.Raw(@sp)'), .append(@Html.Raw(sp)) etc but with no success
The
@should only be used when you’re making a transition between html and code. otherwise, while you’re in one or the other you don’t need it. With that said, give this a try:Screenshot http://img849.imageshack.us/img849/3250/razordemo.png
Note that I also enclosed the result of
Html.Rawin quotes. You may need to do more to safe-guard that there are no quotes inspthat may interfere with the wrapping quotes.I have to ask though, that if you have the data up-front and able to be rendered why are you handing it off to the client to render (more error-prone and creating both a latency and a dependancy) and not rendering it yourself? It doesn’t seem efficient…