Escape html with mvc3 razor

2019-07-18 11:40发布

I'm trying to create a link that just triggers a javascript function with parameters set by my mvc model. I have something that looks 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 escape the Html without altering it?

3条回答
甜甜的少女心
2楼-- · 2019-07-18 12:18
<a onclick="$(@string.Format('foo_{0}',Model.bar)).toggle()"></a>
查看更多
甜甜的少女心
3楼-- · 2019-07-18 12:33

Just add brackets, tested it and works fine:

<a onclick="$('foo_@(Model.bar)').toggle()"></a>
查看更多
倾城 Initia
4楼-- · 2019-07-18 12:37

Just add parenthesis:

<a onclick="$('foo_@(Model.bar)').toggle()"></a>
查看更多
登录 后发表回答