Monday, May 20, 2024
77
rated 0 times [  80] [ 3]  / answers: 1 / hits: 24074  / 12 Years ago, wed, april 4, 2012, 12:00:00

I'm using ASP.NET and I have a string of HTML in the database.



I want to get that html into a variable on the client.



If I do this:



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


it works fine, because it's essentially doing



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


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



My initial guess would be to .Replace the raw string to add escapes to the quotes, however both .ToString() and .ToHtmlString() (as Html.Raw returns an IHtmlString) do not produce the same markup as simple Html.Raw().



So I'm at a loss of what best to do.


More From » asp.net-mvc-3

 Answers
21

What about replacing before calling the Html.Rawmethod?



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


UPDATE:



There might be other escape chars in the string coming from the model. For that reason I would recommend replacing the slashes first as well. Of course it all depends on what might come from the server in your model.



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


A sample snippet representing the behavior in the javascript:





//Let's 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)




[#86417] Tuesday, April 3, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;