Binding DropDownList For Dictionary Assistant

I create and populate a dictionary and want to associate it with a drop-down list using the DropDownListFor helper method.

How do I display this dictionary of key and value in the drop-down list?

It looks like I can do something like:

 @Html.DropDownListFor(o => o.key, o => o.value, MyDictionary); 

It seems that the first parameter should be the LINQ operator, which displays the key / value pair, and the second parameter is the dictionary itself.

+6
source share
3 answers

You cannot bind a drop-down list to a dictionary. It does not make sense. To create a drop-down list, you need 2 things: the scalar property to bind the selected value and the collection to bind the drop-down options. You have only the second of these two things, which is a dictionary. Therefore, you cannot use a strongly typed helper.

You can do the following ugliness:

 @Html.DropDownList("SelectedValue", new SelectList(MyDictionary, "Key", "Value")) 

but of course, a much better approach would be to use a representation model:

 public class MyViewModel { public string SelectedValue { get; set; } public SelectList Values { get; set; } } 

which you will fill in in the action of your controller:

 public ActionResult Foo() { Dictionary<string, string> dic = ... var model = new MyViewModel { Values = new SelectList(dic, "Key", "Value") }; return View(model); } 

and finally, in your strongly typed form:

 @model MyViewModel @Html.DropDownListFor(x => x.SelectedValue, Model.Values) 
+10
source

You should try the following:

In the controller

 ViewBag.hour = new SelectList(Hours.Values); void AddHours() { Hours = new Dictionary<int, string>(); Hours.Add(00, "00"); Hours.Add(01, "01"); Hours.Add(02, "02"); } 

In submissions

 <div class="col-md-2"> @Html.DropDownList("Hours", new SelectList(ViewBag.hour.Items), "Hours", htmlAttributes: new { @class = "form-control", placeholder = "Hours" }) </div> 
+1
source

I like your answer Darin. Thanks, it helped me come up with this solution. So far I have only tried it with strings, but it works well. The following is a simple example. I also have extension methods (not shown here) for creating View Model objects from Model Schema objects (for HttpGets) and for creating schematic model objects from View Model objects (for HttpPosts).

Show model

 public class Step4ViewModel { public SelectList HowToBeContactedOptions { get; set; } public string HowToBeContacted { get; set; } } 

Extension method to make things easier and cleaner

 public static class EntityExtensionMethods { public static Step4ViewModel PopulateDropDowns(this Step4ViewModel vm) { var howToBeContactedOptions = new Dictionary<string, string> { {"Email", "Email"}, {"US Mail", "US Mail"} }; vm.HowToBeContactedOptions = new SelectList(howToBeContactedOptions, "Key", "Value", vm.HowToBeContacted); } 

controller

 [HttpPost] public ActionResult Step4(Step4ViewModel vm) { if (!ModelState.IsValid) { return View(vm.PopulateDropDowns()); } } 

View

 @Html.ValidationSummary() <table class="table table-bordered table-striped"> <tr> <td> @Html.LabelFor(x => x.HowToBeContacted) </td> <td> @Html.DropDownListFor(x => x.HowToBeContacted, Model.HowToBeContactedOptions) @Html.ValidationMessageFor(x => x.HowToBeContacted) </td> </tr> </table> 
0
source

Source: https://habr.com/ru/post/898624/


All Articles