Adding SelectListItem manually to SelectList for use in DropDownListFor

When I create a SelecList, I want to be able to add SelecListItem manually, and for this I use this code:

List<SelectListItem> Provinces = new List<SelectListItem>(); Provinces.Add(new SelectListItem() { Text = "Northern Cape", Value = "NC" }); Provinces.Add(new SelectListItem() { Text = "Free State", Value = "FS" }); Provinces.Add(new SelectListItem() { Text = "Western Cape", Value = "WC" }); SelectList lstProvinces = new SelectList(Provinces); 

Instead of this:

 var lstProvinces = new SelectList(new[] { "Northern Cape", "Free State", "Western Cape" }); 

After creating the SelectList, I pass it to DropDownListFor via the ViewBag:

 Html.DropDownListFor(m => m.StartPointProvince, (SelectList)ViewBag.Provinces) 

However, when I create a SelectList using the first method, it does not work. It adds 3 values ​​to the drop-down list, but all values ​​are displayed as: Code * Screenshot with exit

However, when I use the second method, it works fine. I want to use the first method, because I want to be able to specify the text and value for each element.

+6
source share
3 answers

The problem is that the SelectList(IEnumerable) constructor does not accept SelectListItem (at least not like SelectListItem to add to its Items collection). It simply accepts a collection of some arbitrary objects that will be used to create a completely unrelated internal collection of SelectListItem .

If you want, you can use the SelectList(IEnumerable, string, string) constructor SelectList(IEnumerable, string, string) this way:

 List<SelectListItem> Provinces = new List<SelectListItem>(); Provinces.Add(new SelectListItem() { Text = "Northern Cape", Value = "NC" }); Provinces.Add(new SelectListItem() { Text = "Free State", Value = "FS" }); Provinces.Add(new SelectListItem() { Text = "Western Cape", Value = "WC" }); this.ViewBag.Provinces = new SelectList(Provinces, "Value", "Text"); 

It will work. But it is not needed because you create complex SelectListItem elements that will not be used by SelectList - it will treat them like any other data object.

In the same way, you can simply use another simple class instead of SelectListItem :

 public class SelectListModel { public String Text { get; set; } public String Value { get; set; } } ... Provinces.Add(new SelectListModel() { Text = "Northern Cape", Value = "NC" }); 
+13
source

Use a DropDownList and name it the same as the model property name. Mine - "ItemType"

  @Html.LabelFor(model => model.ItemType, new { @class = "control-label" }) @Html.DropDownList("ItemType", (IEnumerable<SelectListItem>)ViewBag.ItemTypes, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.ItemType, null, new { @class = "text-danger" }) 

  var types = new List<SelectListItem>(); types.Add(new SelectListItem() { Text = "Select...", Value = string.Empty }); types.Add(new SelectListItem() { Text = "OTC", Value = "0" }); types.Add(new SelectListItem() { Text = "Generic", Value = "1" }); types.Add(new SelectListItem() { Text = "Brand", Value = "2" }); types.Add(new SelectListItem() { Text = "Non-Merchandise", Value = "9" }); ViewBag.ItemTypes = types; 

  [Required(ErrorMessage = "Item Type is required")] public Int32 ItemType { get; set; } 
+3
source

you can change your code from

 SelectList lstProvinces = new SelectList(Provinces); 

to

 SelectList lstProvinces = new SelectList(Provinces, "Value", "Text"); 

and he will display the provinces correctly.

+1
source

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


All Articles