Escape html with mvc3 razor

I am trying to create a link that simply runs a javascript function with the parameters set by my mvc model. I have something like this

<a onclick="$(' foo_@Model.bar ').toggle()"></a> 

However, this literally makes the onclick element ' foo@Model.bar ' instead of printing the contents of Model.bar. How do I avoid HTML without modifying it?

+2
source share
3 answers

Just add the brackets, test them and do a great job:

 <a onclick="$(' foo_@ (Model.bar)').toggle()"></a> 
+4
source

Just add a parenthesis:

 <a onclick="$(' foo_@ (Model.bar)').toggle()"></a> 
+1
source
 <a onclick="$(@string.Format('foo_{0}',Model.bar)).toggle()"></a> 
0
source

Source: https://habr.com/ru/post/1391973/


All Articles