-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Closed
Labels
DoneThis issue has been fixedThis issue has been fixedarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.This issue describes a behavior which is not expected - a bug.
Milestone
Description
Blazor doesn't render the value
attribute of an option
element if the value is an empty string. In other words, this:
<option value="">Test Option</option>
becomes:
<option>Test Option</option>
when rendered to the client. The result is that selecting the option will set the containing select
element's value to the text of the option (i.e. Test Option
) instead of the empty string.
The same markup, when used inside a .cshtml
file, renders the value
attribute to the client, allowing for an option that has an empty string as its value. I haven't found a way to do this from a .razor
file short of running some Javascript code to set the attribute after the content is rendered.
To Reproduce
- Using v3.0.0-preview8 of ASP.NET Core, create a new Blazor application
- Add the following to the
body
tag of_Host.cshtml
:
<script>
function fixOptionValue(option) {
option.setAttribute('value', '');
}
</script>
- Replace the contents of
Index.razor
with:
@page "/"
@inject IJSRuntime JsRuntime
<select @bind="selectedValue">
<option selected hidden disabled>Select one</option>
<option value="">Default behaviour</option>
<option value="" @ref="EmptyOption" @ref:suppressField>Modified by script</option>
<option>Option without value</option>
</select>
<div>
Selected Value: @selectedValue
</div>
@code {
private string selectedValue;
private ElementReference EmptyOption;
protected override async Task OnAfterRenderAsync(
bool firstRender)
{
if (firstRender)
{
await this.JsRuntime.InvokeAsync<object>(
"fixOptionValue",
this.EmptyOption);
}
}
}
- Run the app and switch between the options in the select element
- Inspect the options in the browser's dev tools
Expected behaviour
Expected the Default behaviour
option to have value=""
coultonluke, hultqvist, mikelyncheski, tripleacoder and valterc
Metadata
Metadata
Assignees
Labels
DoneThis issue has been fixedThis issue has been fixedarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.This issue describes a behavior which is not expected - a bug.