Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:04:27+00:00 2026-05-28T19:04:27+00:00

I am implementing an error handling strategy in an ASP.Net MVC 3 application. I’ve

  • 0

I am implementing an error handling strategy in an ASP.Net MVC 3 application. I’ve written an attribute implementing IExceptionFilter etc. This functions correctly, handling exceptions throw in the action method and returns the exception information serialized as JSON.

I would like to use this attribute to also handle validation errors found by the model binder when passing data to an Action Method. For example, if I POST an object to an Action Method which is deserialized as the Action Method parameter I would like it to throw an exception if a validation error occurs like UpdateModel does. Right now the default model binder seems to behave like TryUpdateModel, simply flipping the ModelState.IsValid rather than throwing an exception.

[ActionExceptionJsonHandler]
public ActionResult CreateSomething(SomethingViewData account)
{
// If model binding fails validation an exception should be thrown and no code is executed here
// Do stuff here
}

If the default model binder threw an exception in the same way as UpdateModel does then the IExceptionFilter would catch it and handle returning the validation error to the client. Without that the developer must write code to check the ModelState etc.

So bottom line I have two related questions:

  1. Is there a way to have the default model binder throw an exception when validation fails?
  2. Any thoughts on using this approach versus manually checking ModelState in each action method?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:04:28+00:00Added an answer on May 28, 2026 at 7:04 pm

    My solution ended up being to implement an ActionFilterAttribute as shown below. In OnActionExecuting I check ModelState.IsValid, if it’s false I serialize the model state errors as JSON and set the Result object effectively canceling execution. This allows for me to return a custom JSON serialized object containing the model binding errors.

        public override void OnActionExecuting(ActionExecutingContext filterContext) {
    
            if (filterContext.Controller.ViewData.ModelState.IsValid) {
                base.OnActionExecuting(filterContext);
                return;
            }
    
            var returnDto = new ReturnDto
                                {
                                    Success = false,
                                    Errors = Tools.GetModelStateErrors(filterContext.Controller.ViewData.ModelState)
                                };
    
            // AllowGet is fine provided we are not returning a javascript array
            filterContext.Result = new JsonResult { Data = returnDto, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen two methods of implementing global error handling in an ASP.NET MVC 3
I'm implementing the Enterprise Library Exception Handling Application Block in an ASP.NET application that
I am implementing a new ASP.NET MVC 3 application that will use a form
I'm implementing the Data Validation Validators as shown here: http://www.asp.net/learn/mvc/tutorial-39-cs.aspx This works great at
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
On my ASP.NET MVC application, I am trying to implement a URL like below
I'm writing a .NET application and the thought of implementing a data layer from
When implementing error-handling using the built-in validation-helpers on a strongly-typed view, you usually create
So I have this idea of creating a asp.net user control, to handle all
I am implementing Ninject dependency injection in an existing MVC 2 application that uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.