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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:42:22+00:00 2026-06-14T16:42:22+00:00

We have a number of forms on our site that are shown with jquery

  • 0

We have a number of forms on our site that are shown with jquery .dialog and we submit them using an ajax post request.

I’ve done a fair bit of research and from what I can tell there isn’t any definite patterns on how to return validation errors from the server side as a result of an ajax request.

Pretty much the only pattern I could find was to post the form and then do validation server side and in either case return a json object that encapsulated a result and if the result was incorrect the form html

ie.

{result: true}
{success: false, html =  "<form>....</form>"}

So in the case the result was false you would need to rewire up any events attached to items on the form as you would be replacing the old form with the new form that has the validation errors.

This seems like an ok approach but you also end up potentially returning a lot more data to the client that you need to when they only really need to validation messages and you are also forced to rewire the form up which is a bit annoying.

I did find one other mention of somehow getting the validation errors and returning them in a json object from the action and somehow telling the client to display them against the correct fields.

Is there any frameworks out there that make this easier or should I write my own or just stick to returning the entire partial for the form and rewiring it when validation is incorrect?

  • 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-06-14T16:42:24+00:00Added an answer on June 14, 2026 at 4:42 pm

    I don’t know of any frameworks that handle this particular case–and I don’t know that there’s a clear best practice–but it’s easy enough to serialize validation errors and return them as a JSON object. You could try this extension method on ModelStateDictionary:

    public static IEnumerable<ValidationResult> GetValidationResults(this ModelStateDictionary dictionary)
    {
        foreach (var key in dictionary.Keys)
            foreach (var error in dictionary[key].Errors)
                if (error != null)
                    yield return new ValidationResult(error.ErrorMessage, new string[] { key });
    }
    

    And in the controller:

    if (!ModelState.IsValid)
    {
        return new JsonResult(ModelState.GetValidationResults());
    }
    

    But you’re right, you would then have to loop through the object and append the errors to the correct fields. If you have ClientValidationEnabled and UnobtrusiveJavaScriptEnabled set to true, the loop would look something like this:

    $.each(errors, function(i, item) {
        $('span[data-valmsg-for=' + item.MemberNames[0] + ']').html(item.ErrorMessage);
    })
    

    If not, it wouldn’t be that difficult to match up the error messages to their respective fields as the object contains the field name. This would definitely save you some data across the wire, but it moves a larger share of the validation responsibility into Javascript. Like I said, I don’t know that there’s a clear best practice, but I have used this method in the past with success.

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

Sidebar

Related Questions

I'm writing an Access database. I have a number of forms that are identical.
We have a backbone.js app that displays a number of forms to the user.
Scenario I have a C# windows forms application that has a number of processes.
I have create a site embedded Google mapusing api V3 that shows our 4
I have a form that needs validating using DOM methods and javascript for our
We have several terminals throughout the plant that employees will be using to submit
We have a number of very old data entry forms that were made in
I have a number of forms like this: <form class=some-name> <input type=hidden name=bill valueflower
I have a simple custom NumberField: class NumberInput(forms.widgets.Input): input_type = 'number' class NumberField(forms.DecimalField): def
I have a form with number of submit type as images. Each image has

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.