I created a new .NET 6 project. And I put the following code in the Index.
<form>
<input type="text" asp-for="Name" />
<input type="submit" />
</form>
<script src="~/lib/jquery/dist/jquery.min.js" ></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js" ></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js" >
</script>
The C # codes are as follows:
public class IndexModel : PageModel
{
public string Name { get; set; }
public void OnGet()
{
}
public void OnPost(string Name)
{
}
}
When I run the project in the browser:
<main b-g6ltozs93r="" role="main" class="pb-3">
<form novalidate="novalidate">
<input type="text" data-val="true" data-val-required="The Name field is required." id="Name" name="Name" value="">
<input type="submit">
</form>
<script src="/lib/jquery/dist/jquery.min.js"></script>
<script src="/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
</main>
I had not Required the name field . How do I fix this problem?
Nullable element configures how the compiler interprets the nullability of types and what warnings are emitted. <Nullable>enable</Nullable>
means Non-nullable unless declared with ?
.Here are two solutions:
1.Try to remove <Nullable>enable</Nullable>
in .csproj file.
2.Add ?
to Name
:
public string? Name { get; set; }
Then you will not have data-val-required="The Name field is required."
in your html code.