The Wayback Machine - https://web.archive.org/web/20220207091837/https://github.com/dotnet/aspnetcore/issues/40026
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
Sausbolle opened this issue Feb 7, 2022 · 0 comments

Comments

@Sausbolle
Copy link

@Sausbolle Sausbolle commented Feb 7, 2022

Is there an existing issue for this?

  • 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: <input type="checkbox" @Html.DisabledIf(true) checked="@(false)" />
Will Work: <input type="checkbox" checked="@(false)" @Html.DisabledIf(true) />

If you use helper function for both checked and disabled it all works

Will Work: <input type="checkbox" @Html.DisabledIf(true) @Html.CheckedIf(false) />
Will Work: <input type="checkbox" @Html.CheckedIf(false) @Html.DisabledIf(true) />

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: <input type="checkbox" @Html.DisabledIf(true) checked="@(false)" />
Will Work: <input type="checkbox" checked="@(false)" @Html.DisabledIf(true) />

If you use helper function for both checked and disabled it all works

Will Work: <input type="checkbox" @Html.DisabledIf(true) @Html.CheckedIf(false) />
Will Work: <input type="checkbox" @Html.CheckedIf(false) @Html.DisabledIf(true) />

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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant