razor-pagesasp.net-core-tag-helpersselectlistitem

Razor Pages SelectListItem not selected


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


Solution

  • 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