C# how to set a class attribute value dynamically in an ActionLink?

500 views Asked by At

I have an ActionLink that looks like this:

@Html.ActionLink(Model.Name, "Details", "Appointments", new { id = Model.Id }, new { @class = "btn btn-default" })

It works, but I would like to enhance it to something that can change the height of the rendered button according to a Size property in the Model. I have not figured out how to write that piece of code and get it to work. I want something like this in "pseudo-code:

@Html.ActionLink(Model.Name, "Details", "Appointments", new { id = Model.Id }, new { @class = "btn btn-default", height=Model.Size })

I hope someone would be able to show how this could should be written for it to work.

2

There are 2 answers

0
Paul Hirvonen On

I found a solution that works, in the direction you pointed ashik! Thank you for your help.

@Html.ActionLink(Model.Name, "Details", "Appointments", new { id = Model.Id }, new { @class = "btn btn-default", @style = @Model.Size })

It works by assigning for instance the string "height: 100px" to the Size property of the viewmodel in the controller.

1
AudioBubble On

Try adding style to your action link

style="height: @(Model.size+"px")" 

@Html.ActionLink(Model.Name, "Details", "Appointments", new { id = Model.Id }, new { @class = "btn btn-default" , @style="height: @(Model.size+"px")" })