3

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?

2 Answers 2

11

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 + ";"
Sign up to request clarification or add additional context in comments.

3 Comments

anyway to do it with out contacting? Makes it longer and not as clean as I would like. I got to do the same for color.
You can use String.Format(..), that will cut down on the actual number of concatenations done in your code. If it's reasonable, you can also use CSS classes instead of in-line styles, that way it would just look like this: @class = Model.CssClass. In this case, the @ symbol is a C# symbol to escape the class keyword, it has nothing to do with the Razor view.
I guess I will try String.Format. I normally would make a class but the BackGround color changes for every row in the table depending on what the user set. So this just seems to be the easier way to do that.
1

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

I think.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.