4

I’m working on an ASP.net Razor Page and having a problem with the tag-helper for a select-list. Especialy with the selected-property of the SelectListItem.

I’ve got a list of Operating Systems from my database which one is assigned via a DeviceId to a device.

When I fireup the Page I assume to have the “devicebound” operating system selected. The device id is published via int id by the Parameter of the OnGet-Method.

This is the code to check if the device has an operating system bound. It will return the db-id of the operating system.

var SelectedOperatingSystemId = await _context.LicenseRelationships
                    .Include(lr => lr.License)
                    .ThenInclude(l => l.Software)
                    .Where(lr => lr.DeviceMetaDataId == id)
                    .Select(x => x.License.Software.Id)
                    .SingleOrDefaultAsync();

Here’s the debuggers output: enter image description here

After that I create an IEnumerable with this peace of code:

OpertaingSystemsList = await _context.Softwares
            .Where(s => s.IsOperatingSystem == true)
            .OrderBy(s => s.Name)
            .Select(s => new SelectListItem
            {
                Value = s.Id.ToString(),
                Text = s.Name,
                Selected = s.Id == SelectedOperatingSystemId
            })
            .ToListAsync();

So, the option of the select should be selected when db-id equals the one’s of the variable SelectedOperatingSystemId. This is the Debugger’s output which is correct and what I was asuming:

enter image description here

But here is the rendered code of the page. Value 2 should have been selected:

enter image description here

That’s the Html markup:

<div class="form-group">
                <label asp-for="OperatingSystemId"></label>
                <select asp-for="OperatingSystemId" asp-items="Model.OpertaingSystemsList" class="form-control">
                    <option value="">Betriebssystem wählen</option>
                </select>
            </div>

I’ve no explanation for this behavior. Could it be a bug? Or does anybody see a mistake in my programming? Thanks, Patrick

Sum1Unknown
  • 1,032
  • 1
  • 9
  • 14

1 Answers1

3

Oh my god! The solution is easy but documented worst. In Razor Pages the SelectListItem property Selected isn't used anymore.

Look here: LearnRazorPages.com

What you have to do is to assign the selected value to the property you're using for the asp-for in the Select list.

This is your property:

[BindProperty]
public int OperatingSystemId { get; set; }

Your Select is consuming this in it's asp-for and this property you have to assign the selected value in my case this is done in the code code behing:

OperatingSystemId = await _context.LicenseRelationships
                    .Include(lr => lr.License)
                    .ThenInclude(l => l.Software)
                    .Where(lr => lr.DeviceMetaDataId == id)
                    .Select(s => s.License.Software.Id)
                    .SingleOrDefaultAsync();

Finally the select markup has to look like this:

<label asp-for="OperatingSystemId"></label>
<select asp-for="OperatingSystemId" asp-items="Model.OpertaingSystemsList">
     <option value="">Choose Operating System</option>
</select>

I hope this will help anybody who got crazy, too

Bellash
  • 7,560
  • 6
  • 53
  • 86
Sum1Unknown
  • 1,032
  • 1
  • 9
  • 14
  • It has NEVER been used in any version of MVC :) - refer [How to set “selectedValue” in DropDownListFor Html helper](https://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper/41731685#41731685) –  Aug 10 '18 at 22:33
  • 1
    The statement (_Setting Selected Item_) in the article you linked to is incorrect - the only time the `Selected` property was respected is when you did not bind to a model property –  Aug 10 '18 at 22:46
  • 1
    this line shows that the Select property is used https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.ViewFeatures/src/DefaultHtmlGenerator.cs#L1133 – Bellash Apr 20 '22 at 18:15