dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.39k stars 10k forks source link

Select Tag Helper Should Always Honor Overrides from an Explicit Multiple Attribute #34602

Open arex388 opened 3 years ago

arex388 commented 3 years ago

Is your feature request related to a problem? Please describe.

In a project I'm working on, I'm offering a global configuration setting (EnableUserMultiComputerAssignment) to allow a User to be assigned to one or more Computers. Based on the setting's value I want to present a single or a multi select. The problem I'm running into is that because I am always passing an IEnumerable for the asp-for attribute, the current implementation of select tag helper always defaults to a multi-select even when I try to override the multiple attribute.

Looking at the source code for select tag helper it always sets _allowMultiple if asp-for is an IEnumerable.

I currently have to do the following to achieve the result I want:

<div>
    <label asp-for="User!.ComputerIds">Computers</label>
    @if (Model.Settings.EnableUserMultiComputerAssignment) {
        <select asp-for="User!.ComputerIds" asp-items="Model.ComputerSelectListItems"></select>
    } else {
        <select asp-for="User!.ComputerId" asp-items="Model.ComputerSelectListItems" name="User.ComputerIds" id="User.ComputerIds">
            <option></option>
        </select>
    }
</div>

And the model looks like this:

public sealed class User {
    private Guid? _computerId;

    public Guid? ComputerId {
        get => _computerId ?? ComputerIds.FirstOrDefault();
        init => _computerId = value;
    }
    public IEnumerable<Guid> ComputerIds { get; init; } = new List<Guid>();
}

Describe the solution you'd like

I want to be able to override the tag helper if I explicitly set the multiple attribute:

<div>
    <label asp-for="User!.ComputerIds">Computers</label>
    <select asp-for="User!.ComputerIds" asp-items="Model.ComputerSelectListItems" multiple="@(Model.Settings.EnableUserMultiComputerAssignment ? "multiple" : null)"></select>
</div>
ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.