Store the Html.Raw () file in a line in Javascript, ASP.NET MVC 3

I am using ASP.NET and I have an HTML string in the database.

I want to get this html in a variable on the client.

If I do this:

var x = '@Html.Raw(myModel.FishValue)'

it works great because it essentially does

var x = '<p>hello!</p>';

however, if there are quotes in the html, it breaks the page.

My initial assumption was: Replace the original string to add screens in quotation marks, however both .ToString()and .ToHtmlString()(since Html.Raw returns IHtmlString) do not create the same markup as simple Html.Raw().

So, I do not understand what is best to do.

+5
source share
3 answers

How about a replacement before calling a method Html.Raw?

 var x = '@Html.Raw(myModel.FishValue.Replace("'","\\'"))' 

UPDATE:

, , escape-. . , , .

 var x = '@Html.Raw(myModel.FishValue.Replace("\\","\\\\'").Replace("'","\\'"))' 

, javascript:

//Let say my Model Content is >  I'd Say \ is a escape character. You can't "Escape"  
    // YOu would have to replace ' --> \' and \ --> \\
    var stringFromServer = 'I\'d Say \\ is a escape character. You can\'t "Escape"'
    alert(stringFromServer)
Hide result
+9

:

var x = '@(System.Web.HttpUtility.HtmlEncode(myModel.FishValue))';

HTML ,

unescape(x)

, JQuery ( , ) HTML, unescape().

+1

-xss Microsoft ( asp.net 4.5):

 AntiXss.JavascriptEncode(yourContent)

Anti-Xss - 4.1. , , : http://weblogs.asp.net/jgalloway/archive/2011/04/28/using-antixss-4-1-beta-as-the-default-encoder-in-asp-net.aspx

+1
source

All Articles