I have two dropdowns on my view that share the same SelectList from the model. The problem I have is if a value is selected for "Primary" and not for "Secondary", when load the page with the selected data both "Primary" and "Secondary" will both have the same selected value. If a value for both are selected, then the correct values will be displayed. How can I get it to not show the value for both?
Example Model:
public class ExampleViewModel
{
public string Primary { get; set; }
public string Secondary { get; set; }
public IEnumerable<string> Categories { get; set; }
}
Example View:
...
@model Categories
...
<div class="form-group">
@Html.DropDownListFor(x => x.Primary, Model.Categories, "", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.DropDownListFor(x => x.Secondary, Model.Categories, "", new { @class = "form-control" })
</div>
...
Example Controller:
public ActionResult Index(string primary, string secondary)
{
return View(new ExampleViewModel {
Primary = primary,
Secondary = secondary,
Categories = context.Query<Categories>().Select(x => new SelectListItem {
Text = x.Name
}));
}
As discussed, add new SelectList(Model.Categories),"Value","Text"). Check the syntax for the overload as per comment.
Thanks