I am adding a partial view in asp.net core / razor pages using ajax()
. When using unobtrusive validation I am not getting the custom error messages nor the field name from the [Display(Name = "Year of Graduation")]
reference in the error back to the client. I simply only get "This field is required."
The following code seems to allow the unobtrusive validation to work:
$ajax({
//code removed for brevity
})
.done(function (response) {
//LOAD PARTIAL
$("#grad-form").html(response);
var form = $("#gaform")
.removeData("validator")
.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
});
ref:https://stackoverflow.com/a/14982495/2343826
However, I want it to get the name of the field from the data annotation in the client error. Display Attribute: [Display(Name = "Year of Graduation")]
As-is error message on invalid field:
"This field is required"
But desired outcome is:
"This Year of Graduation is required."
Note: The desired outcome would work fine if it wasn't loaded dynamically with ajax(). (I think I just need to reset "unobtrusiveValidation"
after .parse(form)
but I do not know how)