J. Lee
J. Lee

Reputation: 183

MVC Core Validation - render as valid initially

I have an MVC 5 web app, and an MVC Core web app, and I am using unobtrusive validation in both.

When you use TextBoxFor and ValidationMessageFor for a required property, the 2 apps behave differently when the property is initially null.

The MVC 5 app renders the input as VALID, and then the client side validation will mark in as invalid after the input is changed or the form is submitted.

However, in the MVC Core app, the input is rendered as INVALID. So when you display a form for a new item, all required fields say "The [property] field is required" initially and is only removed after they enter a value. This is ugly...

How can I make the MVC Core app behave like an MVC 5 app, and render the inputs as valid, and rely on client side validation to mark them as invalid at the appropriate time.

Thanks

Upvotes: 0

Views: 135

Answers (1)

Rena
Rena

Reputation: 36645

The MVC 5 app renders the input as VALID, and then the client side validation will mark in as invalid after the input is changed or the form is submitted.

Comment on this section in your view:

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Result: enter image description here

Upvotes: 0

Related Questions