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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:29:31+00:00 2026-05-11T18:29:31+00:00

I have been looking at many ASP.Net MVC client side validation ideas including xVal.

  • 0

I have been looking at many ASP.Net MVC client side validation ideas including xVal. This doesn’t provide a ValidationSummary at the moment so I chose to do an AJAX post which loops through ModelState errors and update a DIV with the error messages on a successful AJAX post.

The problem with this is your ValidationMessage * next to the fields won’t get populated. I have come up with an alternative idea which I haven’t yet tested as I don’t know the full syntax to get it working yet but thought I’d see what you guys thought.

One issue I think may be an issue is that when you post to your Edit/Create Action method in the controller and you want to return a JSON object, I’m not sure that is legal as JSON is used for GET actions only.

If you think its a good idea and are wanting to help please leave an answer and any code snippets to get this working. If you think its a hair brained scheme and can be done better please let me know how.

Controller:

if (!ModelState.IsValid)
{
            var err = ModelState.Where(f => f.Value.Errors.Count > 0);    
            if (Request.IsAjaxRequest())
            {
                   return this.Json(err);
            }
            else
            {
                  return View(PostedItem); 
            }
}

View:

    $(function() {



    $('#createForm').ajaxForm({
        success:fillSummary
    });

    //click events
    $('#btnSave').click( function(){
        $('#createForm').submit();
    });

    function fillSummary(data) 
    {
       //Loop through the modelstate errors returned
        $.each(data)
       {
            //Append Summary DIV with error message
            //Look for span with the ModelState key name and set it to visible
       }           
    }



    <div id="summary">
       <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.")%>
    </div>
    <form action="<%=Url.Action("Create") %>" method="post" id="createForm">

        <fieldset>
            <div>
                 <label for="Title">Title:</label>
                <%= Html.TextBox("Title",Model.Title) %>
                <%= Html.ValidationMessage("Title", "*") %>
                <span id="val_Title" style="display:none">*</span>              
            </div>
   </form>
   <input type="button" value="Save" id="btnSave" />
  • 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-11T18:29:31+00:00Added an answer on May 11, 2026 at 6:29 pm

    In my current ASP.NET MVC project I have a lot of POST actions that I use for AJAX and ran across the validation issue too. What I did was create a wrapper object that gets returned from each of these actions which looks something like this…

    public class JsonWrapper
    {
       public object Data { get; set; }
       public bool IsError { get; set; }
       public string Message { get; set; }
    }
    

    If the validation in the action doesn’t have any kind of errors, I place whatever data I want to return in the Data property. However, if there’s any kind of validation error or other exception, I set the IsError flag to true and set an error message in the Message property. Then at the end of the action, I serialize the object to JSON and return it, (yes you can do this from a POST action)…

    return Json(myJsonWrapper);
    

    From the client side, in the onSuccess of my AJAX POST code, I check for errors, and take any actions neccessary like this… (Note, at this point in the code, the object that got returned from the server has already been deserialized into a JS object by jQuery)

    function onSuccess(jsonWrapper) {
        if (!jsonWrapper.IsError) {
            var myDataFromAction = jsonWrapper.Data;
            //Do stuff with my data
        }
        else {
            MessageBox.ShowMessage(jsonWrapper.Message);
        }
    }
    

    This won’t fit your scenario out of the box, but you could do something similar as a concept. Hope this at least gives you some ideas.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's based on ActiveResource, so luckily your code will look… May 12, 2026 at 12:57 am
  • Editorial Team
    Editorial Team added an answer But the extra query string variables are never passed. %1… May 12, 2026 at 12:57 am
  • Editorial Team
    Editorial Team added an answer You mean you get the error when doing this? IFoo… May 12, 2026 at 12:57 am

Related Questions

I'm new to nHibernate, and trying to get my head around the proper way
I'm working a digg clone in ASP.NET MVC to help better teach myself ASP.NET
I have an app, which is a bunch of c# web services sitting on
Background Info I am working on setting up a method for my company's developers

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.