Why is JQuery Validate marking an input field required by default in ASP.NET Core MVC

226 Views Asked by At

I have a simple view that is bound to a model where neither property is required (and both are [nullable] strings). By default, the markup generated marks the Question field as required, even though I haven't indicated that this field is required anywhere.

Why is it doing this? What do I need to do to indicate this field isn't required?

The view:

@model EightBallModel
@{
    Layout = null;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Magic 8-Ball Game</title>

    <!-- CSS Includes -->
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

    <style type="text/css">

        .field-validation-error {
            color: #ff0000;
        }

    </style>
</head>

<body>
    <div class="container">
        <div class="col-md-6 col-md-offset-3">
            <h1>Ask the Magic 8-Ball Anything</h1>
    
            <form method="post" asp-action="GetAnswer" asp-controller="Home">
                @Html.AntiForgeryToken()
                <div class="form-group">
                    @Html.LabelFor(m => m.Question)
                    @Html.TextBoxFor(model => model.Question, new {@class="form-control"})
                    @Html.ValidationMessageFor(model => model.Question)
                </div>
                <button type="submit" class="btn btn-success submit">Ask</button>
                <br/><br/>
                @Html.ValueFor(x => x.Answer)
            </form>

        </div>
    </div>

    <!-- JS includes -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

    <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>

</body>
</html>

The model class:

using System.ComponentModel.DataAnnotations;

namespace MagicEightBall.Models
{
    public class EightBallModel
    {
        public string Answer { get; set; } = string.Empty;
        public string Question { get; set; } = string.Empty;
    }
}

The controller action method:

public IActionResult Index()
{
    return View(new EightBallModel());
}

Result:

enter image description here

The mark-up generated:

enter image description here

1

There are 1 best solutions below

0
bperniciaro On

In ASP.net 6 and above, the documentation suggests making string properties nullable to signal that they can be empty:

        public string? Question { get; set; } = string.Empty;

And while this does allow the form to be submitted, the "Question is Required" message still appears.

The only way I could allow the form to be submitted WITHOUT seeing the error message is to disable this behavior in the Program.cs file:

builder.Services.AddControllers(
    options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);