2015-10-15 51 views
0

我有一個頁面,該行:轉義字符在MVC視圖的JavaScript

<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 })'}" />&nbsp; 

@item.name.ToString()中有一個燒毛報價('),我需要逃避它。

當我看原始HTML '&#39

+2

@ item.name.ToString()更換( 「 '」,@ 「\'」) – 2015-10-15 18:07:02

回答

0

您可以使用@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 })'}" /> 
0

正如你可以看到在這Answer使用@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 })'}" />&nbsp;