0

I am migrating my MVC .NET Framework 4.7.2 App to a .NET Core 2.1 App.

My ViewModel is as below:

public class MyViewModel, IValidatableObject
{
    [Required(ErrorMessage = "Please enter a name")]
    public string Name { get; set; }

    //Other props removed for brevity
}

I have an ajax call to save the data on screen which hits an API with a JsonValidationFilter attribute like below:

    [HttpPost]
    [JsonValidationFilter]
    [Route("api/MyController/Id}/Save")]
    public async Task<IActionResult> SaveAsync(int Id, MyViewModel model)
    {
        //code removed for brevity
        _myService.Save();

        return Ok();
    }

So the code in my ValidationFilter for the .NET Framework version of the application is as below:

public class JsonValidationFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid) return;
        var errorMessages = actionContext.ModelState.Values
            .SelectMany(modelState => modelState.Errors.Select(x => x.ErrorMessage));
        actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, Json.Encode(errorMessages));
    }
}

And if I try to save the screen with a name I get into the error function of my Ajax call and the Validation Alert appears and the validation error is contained in the xhr.responseText so it displays as expected.

                error: function (xhr) {

                    $(saveAlertTarget).html('<span class="glyphicon glyphicon-warning-sign"></span>There was a problem with the last save attempt');
                    if (xhr.status == '400') {
                        displayErrorMessage("Please fix validation errors before saving", xhr.responseText);
                    }
                }

I have attempted to rewrite the validation filter in .NET Core as below - and I do get into the error part of my Ajax call with a 400 request so the Validation alert displays but it never builds up the message fully because xhr.responseText is always blank - have I missed something setting this up?

My .NET Core JsonValidationFilter

public class JsonValidationFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext actionContext)
    {
        if (actionContext.ModelState.IsValid) return;
        IEnumerable<string> errorMessages = actionContext.ModelState.Values.SelectMany(modelState => modelState.Errors.Select(x => x.ErrorMessage));
        actionContext.Result = new BadRequestObjectResult(errorMessages);
    }
}
3
  • Can you check in your browser whether the response actually contains a body? Commented Jan 8, 2019 at 16:05
  • @poke - just looking a Dev Tools on Chrome and in Network Tab I do see a response ["Please enter a name"] - but that isnt getting picked up in the error function of my Ajax call Commented Jan 8, 2019 at 16:16
  • Very strange - now it appears that xhr.responseText is getting the data but it isnt getting displayed in alert - will need to look into the displayErrorMessage function Commented Jan 8, 2019 at 16:22

1 Answer 1

1

This was an issue with my displayErrorMessage java script function - the C# .NET Core Json Validation Filter was working as expected

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.