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 411197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:57:48+00:00 2026-05-12T17:57:48+00:00

There is a plenty of very good post and explanations how to implement validation

  • 0

There is a plenty of very good post and explanations how to implement validation with ASP.NET MVC, and I prefer one of these:

  • xVal
  • Validation with Service layer
  • Data Annotation attributes

However, I really like to call ActionMethods via jquery $.ajax method. One of the reasons why I want to use $.ajax is because there will be a lot of partial views loaded into the page dynamically (even the form for entity creation) via $.ajax calls and I can’t just return the view – I’ll lose all dynamically loaded content.

To give you a better view on the problem, I’ll post some simple code to explain how would I like to call controllers actions and handle responses in client, jquery code.

The controllers ActionMethod:

    public ActionResult CreateCustomer(string name, string accountNumber)
    {
        try
        {
            CustomerService.InsertCustomer(name, accountNumber);

            return Json(new ActionInfo()
            {
                Success = true,
                Message = "Customer Is Successfully Created"
            });

        }
        catch (Exception ex)
        {
            return Json(new ActionInfo()
            {
                Success = false,
                Message = ex.Message
            });
        }
    }

Calling and handling in client code:

$.ajax({
type: "POST",
url: $form.attr('action'),// /MyController/CreateCustomer
data: $form.serialize(),
error: HandleUnespectedError,
dataType: "json",
success: function(response) {

    if (response.Success)
        alert("Success: " + response.Message);
    else
        alert("Error: " + response.Message);
}});

Is there a good way to make some of these validation frameworks to work the way I need? I know that I can add validation errors in ActionInfo, and then handle it in client, but that would be already a building of my one validation, I believe.

  • 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-12T17:57:48+00:00Added an answer on May 12, 2026 at 5:57 pm

    I have had great success doing validation via AJAX using data annotations attributes. In order to check the validity of your data, you will want to use the controller’s ModelState property, which has a property of its own called IsValid. I strongly suggest taking a peek at the data annotations validation attributes tutorial from the official ASP.NET MVC site.

    First off, you will want to modify your controller action to accept your model object as a parameter, rather than a separate name and account number. This will make performing the validation, which I will demonstrate below, much simpler. From your example, my best guess is that your model object is, or would be, called Customer. You might have the following code to define the model object and your controller action…

    // model object
    public class Customer
    {
      public Int32 Id {get; set;}
      public String Name {get; set;}
      public String AccountNumber {get; set;}
    }
    
    // controller
    public class CustomerController : Controller
    {
      public ActionResult CreateCustomer( [Bind(Exclude = "Id")] Customer customer )
      {
         // controller action code
      }
    }
    

    Make sure your form fields are named to match the names of the properties of the Customer object so ASP.NET MVC can automagically bind them. The “Bind” attribute, in this case, is telling ASP.NET MVC to ignore the “Id” property of the Customer class when binding form fields to model properties. Since this is a new customer, we don’t have an Id yet, so we can safely leave the Id as whatever the default value is and leave the data layer to figure out how best to generate it.

    Once the controller has constructed the model object for the action method, its validity can be easily checked via the ModelState.IsValid property. As one might expect, it will return true if the model properties are valid, or false if 1 or more properties are invalid.

    From the original question, it appears that the CustomerService.InsertCustomer method is throwing exceptions when validation fails. This is completely unnecessary. InsertCustomer should only need to perform whatever data operations are necessary for inserting the new record. Unless you wish to abstract implementation specific exceptions, like SqlException, InsertCustomer should not really need to catch or throw any exceptions, but can most likely just let any exceptions bubble up to the controller (or whoever the caller may be).

    The end result of all of this, might be a controller action that looks like the following:

    public ActionResult CreateCustomer( [Bind(Exclude = "Id")] Customer customer )
    {
      // model is invalid
      if (!ModelState.IsValid)
      {
        return Json(new ActionInfo()
        {
          Success = false,
          Message = "Validation failed" // you will probably want a more robust message :-)
        });
      }
    
      // service method accepts a Customer object rather than arbitrary strings  
      CustomerService.InsertCustomer(customer);
    
      return Json(new ActionInfo()
      {
        Success = true,
        Message = "Customer created successfully."
      });
    
    }
    

    If you want to report unexpected errors, like database related exceptions, then you can certainly add a try/catch block around the call to InsertCustomer, and return the necessary result for displaying the error message back to the client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really feel that I should learn Lisp and there are plenty of good
As a young professional in .Net, I noticed that there are plenty of .Net
There are plenty of heavyweight tools such as XmlSpy, which are good for prodding
While there is plenty of documentation about XML document structure, there are very few
There are plenty of script/tools for counting line of code, and some to count
There's plenty of information on running Java apps as services, but I need to
There are plenty of PHP frameworks out there as many of you know, and
There are plenty of 'pretty-printing' visualization libraries for Javascript. E.g. those listed here. Googling
Dynamic languages are on the rise and there are plenty of them: e.g. Ruby,
I understand that floating point calculations have accuracy issues and there are plenty of

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.