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

  • Home
  • SEARCH
  • 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 944303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:34:17+00:00 2026-05-15T22:34:17+00:00

I’ve faced the following problem. I’m developing a form for the site and this

  • 0

I’ve faced the following problem. I’m developing a form for the site and this form should have validation. I wanna to use native ASP.NET MVC 2 validation functionality but get stubborn with it. I have a form that is loaded via $.get and displayed using jQuery UI modal dialog. All examples I found explains how to use MVC validation with simple forms and avoid Ajax forms.

I can enable client side validation for this form, but I need to handle server-side validation correctly. How can I handle server-side validation model errors for ajax forms?

  • 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-15T22:34:18+00:00Added an answer on May 15, 2026 at 10:34 pm

    When you pass your object back to the controller, you have to wrap your code in If ModelState.IsValid

    Below is a simplified version of how I edit a user. The first “EDIT” sends the User object TO the View. The second “EDIT” handles the post from the view.

    Function Edit() As ActionResult
        ''# do stuff to populate your User
        Return View(User)
    End Function
    
    <AcceptVerbs(HttpVerbs.Post)> _
    Function Edit(ByVal user as User)
        If ModelState.IsValid Then
            ''# do your valid stuff
        Else
            ''# The posted form was not valid, send the user back
            Return View(user)
        End If
    End Function
    

    Here’s the same thing in C#

    public ActionResult Edit()
    {
        // do stuff to populate your User
        return View(User);
    }
    
    [AcceptVerbs(HttpVerbs.Post)]
    public object Edit(User user)
    {
        if (ModelState.IsValid) {
                // do your valid stuff
        } else {
            //'# The posted form was not valid, send the user back
            return View(user);
        }
    }
    

    EDIT:

    On your view, if you want to add AJAX validation, just add the following.

        <% 
            Html.EnableClientValidation() ''# This is where all the magic happens.  It will build your clientside validation for you out of your MetaData.
            Using Html.BeginForm("Edit", "Users")
        %>
    
          <!-- all your markup crap -->
                <tr>
                    <td>
                        <%: Html.LabelFor(Function(model) model.UserName)%></td>
                    <td>
                        <%: Html.TextBoxFor(Function(model) model.UserName) %>
                        <%: Html.ValidationMessage("UserName", "*")%><br />
                    </td>
                </tr>
    
          <!-- somewhere you'll want to add a Validation Summary of all your errors -->
          <%= Html.ValidationSummary("Oops!, please correct the errors...") %>
    
    
          <% End Using%>
          <!-- bottom of the page -->
    
              <script src="../../Assets/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
              <script src="../../Assets/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
              <script src="../../Assets/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
    

    EDIT:

    Here is some info on rendering using Ajax.BeginForm
    http://singulartechnologies.com/asp-net-mvc-ajax-beginform-sample-code
    http://msdn.microsoft.com/en-us/library/dd381533.aspx
    http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx

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

Sidebar

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.