0

Can anyone please answer to me as why this is not working?

For simplistic purposes propose I have the following:

var raw = "<div style='background:" + color.HexValue + "'></div>";

I am calling it like this:

<td>@Html.Raw(raw)</td>

And it produces the following:

<td><div style=""></div></td>

I have tried so many different ways and nothing seems to have worked. Also could you point me in the right direction to how this should be properly done.

1
  • IF you click View Source, do you really see that? Commented Apr 1, 2011 at 15:21

3 Answers 3

2

It's easy

<td><div style="background: @'color.HexValue'"></div></td>
Sign up to request clarification or add additional context in comments.

Comments

1
<div style="background: @color.HexValue;"></div>

5 Comments

@Tyrone: Are you sure, that your color.HexValue isn't null or empty? Because I tried your code and just changed color.HexValue for my own property and it works without problems. So you should check this too.
Thank you for you prompt response. The string is definitely not null since if I have the following <div>@color.HexValue</div> the value is inserted correctly. Its so strange...
@Tyrone: if the posted solution isn't working, then you will have to provide more code, because I cant see anything wrong in your code so far. Tested it and its working fine for me.
this worked for me <td><div style="background: @(color.HexValue)"></div></td>. Leaving out the brackets caused it to display some weird results. Still getting used to razor syntax.
@Tyroen: Really weird. In my case, it worked only without braces.
0

You're overcomplicating this situation I think. You could write the following in your view and get the right thing:

<td><div style="background: @{color.HexValue}"></div></td>

4 Comments

Except that @{statement} doesn't output anything. If you're going to surround it it should be @(statement)
My mistake - I didn't bother checking before answering =/
I will try this... Thank you for your help. Thought I was going crazy!
This worked brilliantly <td><div style="background: @(color.HexValue)"></div></td>

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.