dotnet / dotnet/aspnetcore

Checkbox always set to checked if using `checked="@(false)"` if Helper function run to set other values first

Open
#40,026 1 comment 0 reactions 0 assignees View on GitHub
area-mvc feature-mvc-razor-views investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

If you create an HTML helper to set disabled flag on checkboxes and simultaneously use `checked="@(false)"` Razor rendering will always set it to true if the helper function is run first but operate as normal if run last.

```
public static class CheckboxHtmlHelper
{
public static IHtmlContent DisabledIf(this IHtmlHelper htmlHelper, bool condition)
{
return new HtmlString(condition ? "disabled=\"disabled\"" : "");
}
public static IHtmlContent CheckedIf(this IHtmlHelper htmlHelper, bool condition)
{
return new HtmlString(condition ? "checked" : "");
}
}
```

If you use helper function for only disabled it the checked will fail if run last
```
Will Fail:
Will Work:
```
If you use helper function for both checked and disabled it all works
```
Will Work:
Will Work:
```

_I know there is better way to create checkboxes, just noticed this strange behavior as I was testing._

### Expected Behavior

Expected behavior would be that the `checked="@(false)"` would set checkbox to false regardless of position in checkbox tag.

### Steps To Reproduce

### Create Helper function for HTML
```
public static class CheckboxHtmlHelper
{
public static IHtmlContent DisabledIf(this IHtmlHelper htmlHelper, bool condition)
{
return new HtmlString(condition ? "disabled=\"disabled\"" : "");
}
public static IHtmlContent CheckedIf(this IHtmlHelper htmlHelper, bool condition)
{
return new HtmlString(condition ? "checked" : "");
}
}
```

### Create Razor/MVC page
If you use helper function for only disabled it the checked will fail if run last
```
Will Fail:
Will Work:
```
If you use helper function for both checked and disabled it all works
```
Will Work:
Will Work:
```

### Exceptions (if any)

_No response_

### .NET Version

5.0.404

### Anything else?

Project is Asp Net Core 3.1.416

`dotnet --info`
```
.NET SDK (reflecting any global.json):
Version: 5.0.404
Commit: 72c68b830a

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19044
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.404\

Host (useful for support):
Version: 5.0.13
Commit: b3afe99225

.NET SDKs installed:
3.1.416 [C:\Program Files\dotnet\sdk]
5.0.210 [C:\Program Files\dotnet\sdk]
5.0.303 [C:\Program Files\dotnet\sdk]
5.0.403 [C:\Program Files\dotnet\sdk]
5.0.404 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.