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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:46:49+00:00 2026-05-13T16:46:49+00:00

I have a modal popup with a form in it. When submitted via ajax

  • 0

I have a modal popup with a form in it. When submitted via ajax (using jQuery) the form is validated and a javascript callback function checks to see if the model was valid. If so, the modal popup is reset and hidden, if not, the html response text is dumped into the modal popup div for the user to see the fault.

I’m doing this in ASP.NET MVC using a jQuery ajaxSubmit and an action that only accepts a POST verb and returns a PartialViewResult. I do this in several places on my site, but am having problems with 2 such workflows off of 1 page. One works, the other doesn’t, using identical code.

Here is the problem:

<% using (Html.BeginForm("ApplyPayment", "SomeController", FormMethod.Post, new { @id = "frmApplyPayment" })) { %>
    <%= Html.AntiForgeryToken(SomeSaltString) %>
    <%= Html.Hidden("ModelValid", ViewData.ModelState.IsValid) %>
    ...
<% } %>

During debugging, even though ViewData.ModelState.IsValid is false when I have validation errors, “#ModelValid” gets set to “True”! I have tried many ways of explicitly setting it to false (like ViewData.ModelState.IsValid ? “True” : “False”). It just always comes out as true even though the variable is false and hides the popup. What is going on?

Here is more code for reference:

function SubmitPaymentForm(form) {
    $(form).ajaxSubmit({ target: "#modalApplyPayment", success: CheckPaymentValidity  });
}

function CheckPaymentValidity(responseText) {
    if ($("#ModelValid").val() != "True") {
        ShowModalPopup("#modalApplyPayment");
    }
    else {
        HideModalPopup("#modalApplyPayment");
    }
}

This all totally works for a very similar workflow off of the same base page for emailing functionality (popup that takes address, validates on ajax post, shows popup if validation error exists).

  • 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-13T16:46:49+00:00Added an answer on May 13, 2026 at 4:46 pm

    The root cause of this problem has to do with how the ModelState is used during a postback and re-rendering of a page. My validation system uses this method:

    TrasnferValidationMessagesTo(ModelStateDictionary modelState,
                                 IList<IValidationResult> results,
                                 FormCollection formValues)
    {
        // Set the ModelValue in modelState for each key in formValues
    
        // Add a ModelError for each error in results
    }
    

    I believe ASP’s engine is designed to utilize the values in the ModelState collection to repopulate a form’s values. This will surreptitiously override my explicit setting of the “#ModelState” hidden field (since hidden fields are captured in form post backs).

    Surprisingly, I found that if my ModelState retained the value of “True” for “#ModelValid” from the previous post back, and I tried to set it explicitly to “False” using code, it would not work. But if I reversed it, starting with “False” and setting to “True”, it worked fine.

    My solution was to add a ModelValid property to my view model and to default it to false and set it to true when there are no validation errors and use that to modify “#ModelValid”. I did not have this same problem with my other modal popup because, there, I used manual validation and the ModelState was never updated with the form values from the previous post back.

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

Sidebar

Related Questions

I'm using ajax requests to populate modal popup windows with data that users have
I have a JQuery modal popup form on a classic ASP page. It's set
Here is the jquery-ui modal code I have been using: //jQuery form dialog modal
I have an ajax modal popup, which is shown on click of a button
I am using Jquery form plugin to upload file in Ajax request.File is successfully
I am using Jquery form plugin to upload file in Ajax request.File is successfully
I have some html to popup a jQuery UI modal: <a href=my/url class=popup>Click me</a>
I'm using FancyBox jquery plugin to display modal popup boxes. The site is: http://www.chatisfying.com
Does anyone have some nice examples of the ajax (modal window) login form popups?
Hi have a JQuery modal popup that I load HTML into. I have the

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.