我认为您不想转义 Razor 字符串:
@Url.Content("~/Content/images/delete_x.png")
请记住,首先解析 Razor 语法。这使得发生了什么更清楚:
@{ var url = Url.Content("~/Content/images/delete_x.png"); }
var row = $("<tr><td><input type='hidden' name='LocationArray' value='"
+ CellValue + "'>" + CellText +
"</td><td class='delete' >" +
"<img src='@url' /></td></tr>");
脚注:一旦你达到了这种动态 HTML 构造水平,可能是时候考虑使用客户端模板库(Underscore、Mustache、$.tmpl 等)了。使用下划线:
<script type='text/template' id='the-template'>
<tr>
<td>
<input type='hidden' name='LocationArray' value='<%= val %>' />
<%= text %>
</td>
<td class='delete' >
<img src='@Url.Content("~/Content/images/delete_x.png");' />
</td>
</tr>
</script>
<script>
var tmpl = _.template($("#the-template").html());
var data = { val: "hello", text: "world" };
var html = tmpl(data);
$("#target").html(html);
</script>