0

(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

7
  • 1
    What version of Visual Studio are you running? I just tried your code and do not get a warning.
    – Kyle
    Commented Dec 12, 2019 at 22:06
  • Visual studio 2019 with code cop package and another extension which checks code installed.
    – ca9163d9
    Commented Dec 13, 2019 at 3:39
  • What is ...omitted... ?? Post the actual code or create a minimal reproducible example Commented Dec 13, 2019 at 10:08
  • @HenkHolterman, it's just some expressions returns bool.
    – ca9163d9
    Commented Dec 13, 2019 at 15:21
  • 1
    @ca9163d9 Is your VS 2019 up to date? I'm on 16.4.1 and do not have this problem.
    – Kyle
    Commented Dec 13, 2019 at 18:49

3 Answers 3

2

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.

2
  • Hmmm that issue is still open and it is specifically stated that the () in @(...) solve it. There probably is another issue overthere that ic closer. Commented Dec 24, 2019 at 20:23
  • 2
    In vs2022 I received the same warning -
    – Sith2021
    Commented Oct 5, 2021 at 16:25
0

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" />
}
4
  • 1
    Blazor should make this unnecessary. Commented Dec 13, 2019 at 16:58
  • If you pass in a false value in Blazor, it will omit the disabled attribute. This answer is misleading.
    – Kyle
    Commented Dec 13, 2019 at 17:14
  • @Kyle, Actualy, you're right, but the compiler warns. The issue is how to avoid compilation warnings. Commented Dec 13, 2019 at 18:44
  • 1
    There should be no compilation warning either, I tried it on visual studio 16.4.1 and do not have the issue.
    – Kyle
    Commented Dec 13, 2019 at 18:48
0

Use a ternary expression:

<input type="checkbox" @bind="@v" @((!isEditing || ...expression omitted...) ? "disabled" : "") />
3
  • Will this generate disabled=disabled or disabled=?
    – ca9163d9
    Commented Dec 13, 2019 at 15:40
  • 1
    @John, not correct... If the expression is evaluated to true, then the attribute disabled is present, else it is missing. The disabled attribute is never assigned a value in HTML. It is either missing or present.
    – enet
    Commented Dec 13, 2019 at 15:50
  • @enet you're right, thanks for pointing that out. I remember some frameworks allow you to do it that way, but seems it doesn't work that way with plain html. I've updated my answer.
    – John
    Commented Dec 13, 2019 at 15:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.