Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Razor with JavaScript 'less than' symbol (<) can't be parsed

I am trying to combine some javascript with server-side variable.

Here I'm trying to replace the "<" sign with the actual less than sign:

@{
  string age = '&lt;1'; // assume I got it from the QueryString 
  <text>$('#select1').val('@age'.replace('&lt;','<'));</text> // and when the parser sees the '<' symbol it thinks I'm trying to close the <text> tage
}

Is there any way that symbol can be escaped/ignored?

like image 393
Arman Bimatov Avatar asked Dec 05 '25 04:12

Arman Bimatov


2 Answers

I ended up using @(Html.Raw("<"))

like image 81
manit Avatar answered Dec 07 '25 20:12

manit


Try this:

Note, it does have to be between script tags for it to work.. (I'm assuming this is the case though)

<script>
@{
  string age = '&lt;1'; // assume I got it from the QueryString 
  @: $('#select1').val('@age'.replace('&lt;','<')); // and when the parser sees the '<' symbol it thinks I'm trying to close the <text> tage
}
</script>

Update

Incidently, the text syntax you used above will also work (instead of @:).. specifically what is causing the problem in your example is not the < in the javascript, rather the second

<text>

in the comment

like image 25
Martin Booth Avatar answered Dec 07 '25 20:12

Martin Booth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!