0

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)

Fraze
  • 908
  • 2
  • 8
  • 20
  • what does `response` contain? is it the whole form? – Neville Nazerane Dec 12 '18 at 23:34
  • It contains only the partial's html.. #gaform is the entire form. Thx! – Fraze Dec 13 '18 at 00:41
  • refreshing the entire form worked fine for me in this project (that also contains angular) https://github.com/neville-nazerane/netcore.angular. You can check the `website` project at the path /angular/a2. Here is the js file which was mostly jquery https://github.com/neville-nazerane/netcore.angular/blob/master/Website/wwwroot/bower/js/netcore-angular.js – Neville Nazerane Dec 13 '18 at 01:10

0 Answers0