Answered by:
selectlist not selecting selected but only for a specific list in ASP.net core v3.1

Question
-
User-842134713 posted
apologies if this has been answered before but the search function was infinitely spinning
i have written a routine that looks like this
public List<GenericSelectListVM> ConvertForSelectList(string stringInput, string defaultChoice="") { var vals = stringInput.Split(",").ToList(); List<GenericSelectListVM> list = new List<GenericSelectListVM>(); foreach (var v in vals) { var select = new GenericSelectListVM() { text = v.Trim(), value = v.Trim(), selected = (defaultChoice == v.Trim() ? true : false) }; list.Add(select); } return list; }
where GenericSelectListVM looks like this :
public class GenericSelectListVM { public string value { get; set; } public string text { get; set; } public bool selected { get; set; } }
and here is the view
@{ if (Model.Hair != null) { <select asp-for="@Model.HairColor" asp-items='@(new SelectList(Model.Hair, "text", "value"))'></select> } }
so stringInput = "brunette,blonde,redhead,black,grey,dyed" with defaultChoice="blonde"
this all works greatand when i do this view
@{ if (Model.ShirtSizes != null) { <select asp-for="@Model.Shirt" asp-items='@(new SelectList(Model.ShirtSizes, "text", "value"))'></select> } }
so stringInput = "S,M,L" with defaultChoice="S"
this all works great toobut then when i do this
@{ if (Model.ShoeSizes != null) { <select asp-for="@Model.Shoe" asp-items='@(new SelectList(Model.ShoeSizes, "text", "value"))'></select> } }
so stringInput = "5,6,7" with defaultChoice="7"
this does not work - the selectlist gets built but the 7 does not get chosenhere is what ConvertForSelectList returns
selected false bool text "brunette" string value "brunette" string selected true bool text "blonde" string value "blonde" string etc selected false bool text "6" string value "6" string selected true bool text "7" string value "7" string etc
here is the actual markup (minus extraneous stuff)
<select data-val="true" id="HairColor" name="HairColor"> <option value="brunette">brunette</option> <option selected="selected" value="blonde">blonde</option> <------------------ good <option value="redhead">redhead</option> <option value="black">black</option> <option value="grey">grey</option> <option value="dyed">dyed</option> </select>
<select id="Shirt" name="Shirt">
<option selected="selected" value="S">S</option> <------------------ good
<option value="M">M</option>
<option value="L">L</option>
</select>
<select id="Shoe" name="Shoe">
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option> <------------------ missing/annoying!!!
</select>any ideas why the shoe size might be problematic?
you can see - even tho the sizes are numbers they have been pre-cast to strings
but that is the only diff i can see between the first two lists and the last
Wednesday, October 28, 2020 4:20 AM
Answers
-
User-842134713 posted
hello - sorry for the late reply
im only just seeing this response
while i understand your viewpoint -
this particular usage was not as cookie cutter as just dbcontexting and model-binding a selectlist
i wish it were bc then i wouldnt have had to come on this forum and ask a question
i do have other selectlists that do that but not this weird one
in this end this all worked out on its own
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, November 13, 2020 12:47 AM
All replies
-
User475983607 posted
To be honest, the code is not somehting you would find in a production application. Generally, a list of user options is generated from a table not a CSV string passed to a method. Also, the framework already has classes to handle user options. I recommend the following Blog as it illustrates standard programming patterns for populating a select to model binding the user's selection.
https://www.learnrazorpages.com/razor-pages/forms/select-lists
Wednesday, October 28, 2020 1:00 PM -
User-842134713 posted
hello - sorry for the late reply
im only just seeing this response
while i understand your viewpoint -
this particular usage was not as cookie cutter as just dbcontexting and model-binding a selectlist
i wish it were bc then i wouldnt have had to come on this forum and ask a question
i do have other selectlists that do that but not this weird one
in this end this all worked out on its own
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, November 13, 2020 12:47 AM