I am trying to dynamically generate the "value" attribute of the tag using the HTML.DropDownListFor, but I am running into issues. I am thinking of just hard-coding a for-loop and inserting raw HTML, but I am positive there is an easier way to do this (?)
I have a view model:
public class DSDCustomerViewModel
{
public IEnumerable<dsd_l_chain> chainBuild { get; set; }
public string Id_dsd { get; set; }
public string Owner { get; set; }
public IEnumerable<string> WFCategory { get; set; }
public IEnumerable<string> TransactionType { get; set; }
[etc etc etc]
the chainBuild property comes from my database (using EF) that I fill using an extension method for my view as follows:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<table>
<tbody>
[etc etc]
<td colspan="1" rowspan="1">
Chain Name
</td>
<td colspan="1" style="vertical-align: top;">
@Html.DropDownListFor(model => model.cc_chainName, new SelectList(Model.cc_chainName), "Select ...")
</td>
<td rowspan="1" colspan="2" style="vertical-align: top;">
@Html.ValidationMessageFor(model => model.cc_chainName)
</td>
[etc etc]
the cc_chainName EF object has the attributes "Chain_Desc", "Chain", "Chain_Group" and "ID" among others.
The generated HTML is as follows:
<select id="cc_chainName" name="cc_chainName"><option value="">Select ...</option>
<option>1ST STOP</option>
<option>3 RIVERS ICE CREAM SPCLTS</option>
<option>3RIVERS SALES TO NON-NAP CUSTS</option>
How do I modify:
@Html.DropDownListFor(model => model.cc_chainName, new SelectList(Model.cc_chainName), "Select ...")
...so that the generated HTML code set the "value" attribute of the tag to cc_chainName.ID? so that the generated HTML would look like this:
<select id="cc_chainName" name="cc_chainName"><option value="">Select ...</option>
<option value="1">1ST STOP</option>
<option value="2">3 RIVERS ICE CREAM SPCLTS</option>
<option value="3">3RIVERS SALES TO NON-NAP CUSTS</option>
note the "value" tags contain the IDs from the cc_chainName model
Thanks!