Kamel Yehya
Kamel Yehya

Reputation: 33

Injecting C# code inside HTML tag helpers

I have an ASP.NET Core project, and in my Razor page, I have an HTML div element:

<div class="progress-bar" role="progressbar" 
     style="background-color: #1ebba3; width: 25%" aria-valuenow="3" 
     aria-valuemin="1" aria-valuemax="4">
</div>

I want to adjust the width of the progress-bar, and I don't want to hardcode the width (25%) into the style. I want to write the width based on a variable I have in an object in the model. How do I do this?

I can get the value from the Model like this:

@{ Model.Obj.Width }

But how can I inject it inside the HTML tag helper?

Upvotes: 0

Views: 425

Answers (1)

bwakabats
bwakabats

Reputation: 703

Use ( ) instead of { }

style="background-color: #1ebba3; width: @(Model.Obj.Width)%"

Upvotes: 1

Related Questions