1

I'm trying to do input validation for an @html checkbox, but given that Razor uses ToString for bools and the like, everything comes out as capitalized. This means that my input validation range and html

[Range(typeof(bool), "true", "true", ErrorMessage = "Residency is required")]

@Html.CheckBoxFor(m => m.IsResident, new { @id = "residentChk" })

Ends up displaying in the source as

<input data-val="true" data-val-range="Residency is required" data-val-range-max="True" data-val-range-min="True" data-val-required="The IsResident field is required." id="residentChk" name="IsResident" type="checkbox" value="true" /><input name="IsResident" type="hidden" value="false" />

With the main culprits of the issue being

data-val-range-min="True" data-val-range-min="True"

Is there anyway to get razor to put these value in as lower case values, or to somehow modify the checkbox to return an upper case value? Im trying to keep parity with the other parts of the code, so I dont want an edge case that handles this with one off js functions if at all possible.

orangejsx
  • 11
  • 2
  • I'm not really sure this is an issue. It's well known that ASP.NET does this, as I can find it in many posts on the interweb, and it doesn't seem like it creates any problems. https://dotnetfiddle.net/JbPh0X . The underlying reason has to do with how C# converts a boolean to a string: https://stackoverflow.com/questions/491334/why-does-boolean-tostring-output-true-and-not-true – EspressoBeans Feb 08 '22 at 17:00
  • Interesting. The dotnet fiddle definitely has the uppercase True in there, yet still works. Maybe my issue is somewhere else in my code. Thanks for the reply! – orangejsx Feb 08 '22 at 18:04

0 Answers0