Search code examples
c#asp.net-corerazortag-helpers

Select Tag Helper not Rendering Attributes


I'm trying to generate a select using the tag helpers in ASP.NET Core, but it seems to struggle with "dynamic" attributes. For example, if I have this:

//  f is some model

var required = f.IsRequired ? "required" : string.Empty;// true for example
var multiple = f.IsMultiple ? "multiple" : string.Empty;// true for example

<select asp-items="@f.SelectItems" @multiple @required>
</select>

I expect to get:

<select multiple required>
    <option></option>
    ...
</select>

But instead I get:

<select>
    <option></option>
    ...
</select>

Is there some way to make my expectation happen?


Solution

  • The answer is really from Kirk, but I'll summarize it.

    For the required and multiple attributes to be rendered they must have a value of true and for them not to be rendered they must have a value of null.