Home DropDownListFor setting value of other property when using shared SelectList
Reply: 0

DropDownListFor setting value of other property when using shared SelectList

user1372
1#
user1372 Published in April 26, 2018, 5:27 am

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 IEnumberable<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
           }));
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304067 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO