ca9163d9
ca9163d9

Reputation: 29159

Warning '@(!isEditing || ...)' is not a valid value of attribute 'disabled'?

(Tried both in Visaul Studio 2019 V16.4.0 or V16.4.1. Microsoft.CodeAnalysis.FxCopAnalyzers added)

I'm using the following code in Blazor to disable inputs.

<input type="checkbox" @bind="@v" disabled="@(!isEditing || ...expression omitted...)" />

However, Visual Studio warns that

Warning '@(!isEditing || ...)' is not a valid value of attribute 'disabled'

What's the proper way to disable the input by custom logic?

Warnings

Upvotes: 0

Views: 2793

Answers (3)

ca9163d9
ca9163d9

Reputation: 29159

It's a bug.

HTML warning when setting disabled attribute using Razor syntax #16833 https://github.com/aspnet/AspNetCore/issues/16833

Update:

All the warnings are disappeared after my Visual Studio is upgraded to V16.4.2.

Upvotes: 2

John
John

Reputation: 579

Use a ternary expression:

<input type="checkbox" @bind="@v" @((!isEditing || ...expression omitted...) ? "disabled" : "") />

Upvotes: 0

agua from mars
agua from mars

Reputation: 17404

html disabled attribute acceptable value is disabled or no value.
The easiest way is to use an @if statement:

@if(!isEditing || ...omitted...)
{
   <input type="checkbox" @bind="@v" disabled />
}
else
{
   <input type="checkbox" @bind="@v" />
}

Upvotes: 0

Related Questions