I have a View that uses unobtrusive ajax to submit a form or rather just normal jquery ajax would do the trick as well. For both implementations, I can get it to hit my controller fine and does my ModelState.IsValid validation and then returns my view like so:
public ActionResult SubmitForm(ViewModel viewModel)
{
if(!ModelState.IsValid)
{
return View("~/view path", viewModel);
}else
{
// do stuff
// I have to return this in order for me to get my model validation errors to show up
return Json(new { msg = "OK" });
}
}
View:
<form asp-action="SubmitForm" asp-controller="Controller" data-ajax="true" data-ajax-method="POST"
data-ajax-success="onSubmitSuccess(data, 'formId')" id="formId">
@Html.ValidationSummary()
// some form fields with html tag helpers
</form>
Now when my SubmitForm action returns my view on failed model validation, my validation summary never shows up. The only way I've got it to work is by replacing my whole forms html with my returned data like so in my OnSubmitSuccess function by checking the json msg property:
function onSubmitSuccess(data, form)
{
if(data.msg)
{
if(data.msg == "OK") { // do stuff }
}else
{
$("#" + form).html(data.responseText);
}
}
I'm basically replacing my forms html with the incoming response text in order for my error messages to show up. Is there a particular reason why I can't just do a return Partial or return View and work without tinkering with jquery and replacing html?
Thanks