htmlasp.net-mvcasp.net-mvc-views

Use variable in style, without spaces


I have this code:

<div class="testexample" style="top:@example.Y px"></div>

I thought this would work fine, but the browser is complaining about the spaces in the style.

(Invalid property value)

Any ideas to solve this?


Solution

  • You can put the whole string inside an expression syntax like this:

    <div class="testexample" style="top:@(example.Y + "px")"></div>