0

i have a foreach to iterate through items in razor syntax,

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ColorCode)
        </td>
        <td>


            <div style="background-color:*************">&nbsp;&nbsp;&nbsp;</div>


        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
    </tr>
}

ColorCode is an int value ,i define an external function named IntToColorHex to convert ColorCode to equivalent hex value,i want to replace ************** with an hex color value for each row according to ColorCode,i need some thing like bellow:

<div style="background-color:IntToColorHex(@item.ColorCode)">&nbsp;&nbsp;&nbsp;</div>
7
  • you can just do tostring in razor without js function, see msdn, so enough something like background-color:#@item.ColorCode.ToString("X6") Commented Aug 16, 2015 at 15:18
  • thanks,but i create IntToColorHex function in external .js file that linked to my view ,item.ColorCode must pass to this function to calculate new value and then set it to background-color Commented Aug 16, 2015 at 15:22
  • 1
    you can't call js function inside html style attribute Commented Aug 16, 2015 at 15:23
  • ok , thanks to your help Commented Aug 16, 2015 at 15:24
  • Why you want call js function, instead just render needed value on server? Commented Aug 16, 2015 at 15:25

1 Answer 1

1

You can't call js function from style attribute, so this line

<div style="background-color:IntToColorHex(@item.ColorCode)">&nbsp;&nbsp;&nbsp;</div>

just render to something like

<div style="background-color:IntToColorHex(100000)">&nbsp;&nbsp;&nbsp;</div>

But you can use simple ToString with hexadecimal ("X") format specifier like

<div style="background-color:#@item.ColorCode.ToString("X6")">&nbsp;&nbsp;&nbsp;</div>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.