MVC视图JavaScript中的转义字符
我有一页写着这行:MVC视图JavaScript中的转义字符,javascript,asp.net-mvc,razor,Javascript,Asp.net Mvc,Razor,我有一页写着这行: <input type="button" title="Join" value="Join" onclick="if(confirm('Are you sure you want to join @item.name.ToString() ?')) {location.href='@Url.Action("JoinTeam", "Team", new { id = item.id })'}" /> @item.name.ToStrin
<input type="button" title="Join" value="Join" onclick="if(confirm('Are you sure you want to join @item.name.ToString() ?')) {location.href='@Url.Action("JoinTeam", "Team", new { id = item.id })'}" />
@item.name.ToString()
中有一个单引号(“
),我需要将其转义
当我查看原始HTML时,”
是'
,正如您在本例中使用@HTML.raw(jsString)
所看到的那样
<input type="button" title="Join" value="Join" onclick="if(confirm('Are you sure you want to join @Html.Raw(item.name.ToString()) ?')) {location.href='@Url.Action("JoinTeam", "Team", new { id = item.id })'}" />
您可以使用
@Json.Encode
转义特殊字符:
<input type="button" title="Join" value="Join"
onclick="if(confirm('Are you sure you want to join @Json.Encode(item.name) ?')){location.href='@Url.Action("Home", "Index", new { id = item.id })'}" />
@item.name.ToString()替换(“,@“\”)