How to do an inline style with asp.net mvc 3 razor in a html helper

23,700

Solution 1

You are already inside a code block; Razor does not parse within code blocks for other code blocks. The style part of the line should look something like this:

style = "width: 20px; background-color: " + Model.BackgroundColor + ";"

Solution 2

you should concat your string like "width: 20px; background-color: " +Model.BackgroundColor + ";"

I think.

Share:
23,700
chobo2
Author by

chobo2

Updated on July 05, 2022

Comments

  • chobo2
    chobo2 almost 2 years

    I want to do this

    @Html.TextBoxFor(x => x.BackgroundColor, new { style = "width: 20px; background-color: @Model.BackgroundColor;" })
    

    Hoever it does not render what is in my Mode.Background color(in firebug I just see @bModel.BackgroundColor"). Is this possible?