So I'm trying to stylize the tag in a dropdown. My MVC helper is set up kind of like this but the real thing pulls from the DB, so the data is not hard coded.
@{
List<string> ListOfText = new List<string> { "FirstThing","SecondThing","ThirdThing"};
List<string> ListOfValue = new List<string> { "1","2","3"};
List<SelectListItems> ListOFSELCETLISTITEMS = new List<SelectListItem>();
for (int x = 0; x < 3; x++)
{
ListOFSELCETLISTITEMS .Add(new SelectListItem
{
Text = ListOfText[x],
Value = ListOfValue[x],
Selected = (selectedValue == ListOfValue[x])
});
}
}
@Html.DropDown("NAME",ListOFSELCETLISTITEMS)
This gives me something like
<select id="Name" name="Name">
<option value="1">FirstThing</option>
<option value="2">SecondThing</option>
<option value="3">ThirdThing</option>
</select>
What i need is something like this
<select id="Name" name="Name">
<option value="1" class="option1">FirstThing</option>
<option value="2" class="option2">SecondThing</option>
<option value="3" class="option3">ThirdThing</option>
</select>
I've tried doing things like
@{
List<string> ListOfText = new List<string> { "FirstThing","SecondThing","ThirdThing"};
List<string> ListOfValue = new List<string> { "1","2","3"};
}
<select id="Name" name="Name">
@ for (int x = 0; x < 3; x++)
{
<text>
<option value="@ListOfValue[x]" @{if(selectedValue == ListOfValue[x])
{ @Html.Raw("selected='selected'") }}
@class = '@Html.Raw("option"+x)'>
@ListOfText[x]
</option>
</text>
}
</select>
But this seems to confuse the controller and it doesn't recognize that the dropdown's value above maps to
public ActionResult method(string Name)
in the controller on a post or get. Well the line
@Html.DropDown("NAME",ListOFSELCETLISTITEMS)
in the view does allow the controller to understand the method should map to that dropdown's value.
How can i do this? Is there a way to unconfuse the controller and be able to hand write HTML without the Html.helpers?