leora
leora

Reputation: 196689

In asp.net-mvc, what is the correct way to handle double quotes in a string variable being put into an input value

I have the following code in my view of an asp.net-mvc site:

<input type="hidden" id="test" name="riskIssues[0].Reason" value="<% = risk.Reason %>"  />

The issue is that if there is a double quote inside the risk.Reason variable lets say the reason is a string that is something like:

  We need them to "ask" us first

and then i try to read the value using jquery ($("#test").val(), i only get the text that is before the double quote (in the example above, i would get

  We need them to 

What is the correct way to encode this so I can read the whole string including the double quotes and everything after

Upvotes: 0

Views: 1259

Answers (2)

Peter B
Peter B

Reputation: 24255

You can use <%: risk.Reason %> to encode your text.

Reference: http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2

Upvotes: 1

JanR
JanR

Reputation: 6132

You could use HttpServerUtility.HtmlEncoding or WebUtility.HtmlEncode to html encode your data received in your view. As shown in the examples here (MSDN) & here (MSDN)

Your string would end up looking like this:

 We need them to "ask" us first

 We need them to &quot;ask&quot; us first

Also have a look here (dotnetperls)

Fiddle

Upvotes: 1

Related Questions