We've started refactoring some web forms to .NET6 (MVC C# Razor)
I am doing nothing that I am aware of to make my optional controls required, yet the browser is requiring them nonetheless. How can I make this control optional?
in the C# class:
[Display(Name = "Web link for more info")]
[Required(AllowEmptyStrings = true)] // I tried removing this but it makes no difference
public string MoreInfoURL { get; set; } = "";
in the Razor cshtml:
@Html.LabelFor(model => model.MoreInfoURL)
<span >Web address to a page that provides more information for interested audiences</span>
@Html.TextBoxFor(model => model.MoreInfoURL, new {placeholder = "(optional)"})
the generated html
<label for="MoreInfoURL">Web link for more info</label>
<span >Web address to a page that provides more information for interested audiences</span>
<input data-val="true" id="MoreInfoURL" name="MoreInfoURL" placeholder="(optional)" type="text" value="">
The result: when I try to submit the form with this control empty, browser jumps up and highlights the control like it is requiring me to enter it - even though I've done nothing to make it required (they worked properly in the previous version of .NET)
What am I doing wrong?
CodePudding user response:
2.Try to add ?
to MoreInfoURL
and remove Required
attribute.
[Display(Name = "Web link for more info")]
public string? MoreInfoURL { get; set; } = "";