ASP.NET MVC 3 List <T> in IEnumerable <SelectListItem>

I'm currently not happy that my DropDownListFor () objects are full. I am trying to find as general a way to populate IEnumerable as possible. This is what I still have.

Helper:

public static List<SelectListItem> ToSelectList(IDictionary<string,string> dictionaryItems, string selectedValue, string noSelection, bool search = false) { List<SelectListItem> items = new List<SelectListItem>(); if (search) { items.Add(new SelectListItem { Selected = true, Value = "-1", Text = string.Format("-- {0} --", noSelection) }); } foreach (var item in dictionaryItems) { items.Add(new SelectListItem { Text = item.Key, Value = item.Value, Selected = selectedValue == item.Value ? true : false }); } return items .OrderBy(l => l.Text) .ToList(); } 

Controller:

 [HttpGet] public ActionResult Index() { var model = new CreateModel(); var parentOrganisations = _orgs.FindBy(o => o.OwningOrganisationID == Globals.OrganisationID || o.ID == Globals.OrganisationID) .OrderBy(o => o.OrganisationName); Dictionary<string, string> items = new Dictionary<string, string>(); foreach (var item in parentOrganisations) { items.Add(item.OrganisationName, item.ID.ToString()); } model.Organisations = SelectLists.ToSelectList(items, "-1", "-- None -- ", true); return View(model); } 

View:

 <div class="control-group"> <label class="control-label">Parent Organisation</label> <div class="controls"> @Html.DropDownListFor(m => m.ParentOrganisationID, Model.Organisations, new { @class = "input-xlarge"}) <p class="help-block">Select a parent organisation to create a branch</p> </div> </div> 

There seems to be a lot of duplicate code in the controller. It takes a general list, adds the value and text to the dictionary, and then uses it as an input for an assistant, which creates a selection list to send as part of the model.

Is there anyone better ways to achieve this? I hate bloating in my controller, and when I get a few drops down in shape, this is exactly what will happen in this case.

Thanks,

EDIT. Thanks to Kenneth’s assistant method, I now combined all this into one call in the controller:

  model.Organisations = _orgs.FindBy(o => o.OwningOrganisationID == Globals.OrganisationID || o.ID == Globals.OrganisationID) .OrderBy(o => o.OrganisationName) .ToList() .ToSelectList(org => org.OrganisationName, org => org.ID.ToString(), "-1", "None", true); 
+6
source share
2 answers

You can provide callbacks that get the key and value, and then use them. Alternatively, you can create it as an extension method:

Extension Method:

 public static List<SelectListItem> ToSelectList<T>(this List<T> Items, Func<T, string> getKey, Func<T, string> getValue, string selectedValue, string noSelection, bool search = false) { List<SelectListItem> items = new List<SelectListItem>(); if (search) { items.Add(new SelectListItem { Selected = true, Value = "-1", Text = string.Format("-- {0} --", noSelection) }); } foreach (var item in Items) { items.Add(new SelectListItem { Text = getKey(item), Value = getValue(item), Selected = selectedValue == getValue(item) ? true : false }); } return items .OrderBy(l => l.Text) .ToList(); } 

Application:

 List<Org>() parentOrganisations = // fetch here model.Organisations = parentOrganisations.ToSelectList(org => org.ID.ToString(), org => org.OrganisationName, "-1", "-- None -- ", true); 

Note. I typed this in an SO editor, so you might have some syntax errors (they should be easy to solve, though).

+8
source

You can only do the following in the controller: -

 List<SelectListItem> dropdownItems = parentOrganisations .Select(item => new SelectListItem { Value = item.ID.ToString(), Text = item.OrganisationName, Selected = "-1" == item.ID.ToString() ? true : false }) .ToList(); 
+1
source

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


All Articles