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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:06:06+00:00 2026-06-16T13:06:06+00:00

I have simple ASP.NET MVC action like this : public ActionResult Edit(EditPostViewModel data) {

  • 0

I have simple ASP.NET MVC action like this :

public ActionResult Edit(EditPostViewModel data)
{

}

The EditPostViewModel have validation attributes like this :

[Display(Name = "...", Description = "...")]
[StringLength(100, MinimumLength = 3, ErrorMessage = "...")]
[Required()]
public string Title { get; set; }

In the view I am using the following helpers :

 @Html.LabelFor(Model => Model.EditPostViewModel.Title, true)

 @Html.TextBoxFor(Model => Model.EditPostViewModel.Title, 
                        new { @class = "tb1", @Style = "width:400px;" })

If I do a submit on a form that this textbox is placed in a validation will be done first on client and then on service(ModelState.IsValid).

Now I got a couple of questions :

  1. Can this be used with jQuery ajax submit instead? What I am doing is simply remove the form and on clicking the submit button a javascript will gather data and then run the $.ajax.

  2. Will the server side ModelState.IsValid work?

  3. How can I forward validation problem back to the client and present it as if Im using the build int validation(@Html.ValidationSummary(true))?

Example of Ajax call :

function SendPost(actionPath) {
    $.ajax({
        url: actionPath,
        type: 'POST',
        dataType: 'json',
        data:
        {
            Text: $('#EditPostViewModel_Text').val(),
            Title: $('#EditPostViewModel_Title').val() 
        },
        success: function (data) {
            alert('success');
        },
        error: function () {
            alert('error');
        }
    });
}

Edit 1:

Included on page :

<script src="/Scripts/jquery-1.7.1.min.js"></script>
<script src="/Scripts/jquery.validate.min.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
  • 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-16T13:06:07+00:00Added an answer on June 16, 2026 at 1:06 pm

    Client Side

    Using the jQuery.validate library should be pretty simple to set up.

    Specify the following settings in your Web.config file:

    <appSettings>
        <add key="ClientValidationEnabled" value="true"/> 
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
    </appSettings>
    

    When you build up your view, you would define things like this:

    @Html.LabelFor(Model => Model.EditPostViewModel.Title, true)
    @Html.TextBoxFor(Model => Model.EditPostViewModel.Title, 
                                    new { @class = "tb1", @Style = "width:400px;" })
    @Html.ValidationMessageFor(Model => Model.EditPostViewModel.Title)
    

    NOTE: These need to be defined within a form element

    Then you would need to include the following libraries:

    <script src='@Url.Content("~/Scripts/jquery.validate.js")' type='text/javascript'></script>
    <script src='@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")' type='text/javascript'></script>
    

    This should be able to set you up for client side validation

    Resources

    • http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvccustomvalidation_topic5.aspx

    Server Side

    NOTE: This is only for additional server side validation on top of jQuery.validation library

    Perhaps something like this could help:

    [ValidateAjax]
    public JsonResult Edit(EditPostViewModel data)
    {
        //Save data
        return Json(new { Success = true } );
    }
    

    Where ValidateAjax is an attribute defined as:

    public class ValidateAjaxAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.HttpContext.Request.IsAjaxRequest())
                return;
    
            var modelState = filterContext.Controller.ViewData.ModelState;
            if (!modelState.IsValid)
            {
                var errorModel = 
                        from x in modelState.Keys
                        where modelState[x].Errors.Count > 0
                        select new
                               {
                                   key = x,
                                   errors = modelState[x].Errors.
                                                          Select(y => y.ErrorMessage).
                                                          ToArray()
                               };
                filterContext.Result = new JsonResult()
                                           {
                                               Data = errorModel
                                           };
                filterContext.HttpContext.Response.StatusCode = 
                                                      (int) HttpStatusCode.BadRequest;
            }
        }
    }
    

    What this does is return a JSON object specifying all of your model errors.

    Example response would be

    [{
        "key":"Name",
        "errors":["The Name field is required."]
    },
    {
        "key":"Description",
        "errors":["The Description field is required."]
    }]
    

    This would be returned to your error handling callback of the $.ajax call

    You can loop through the returned data to set the error messages as needed based on the Keys returned (I think something like $('input[name="' + err.key + '"]') would find your input element

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

Sidebar

Related Questions

I have a simple Edit action in ASP.NET MVC that looks like this :
I have an ajax form in asp.net mvc which is as simple as this:
I have following simple query in ASP.NET MVC action method. IEnumerable<Company> query = unitOfWork.CompanyRepository.dbSet
I created a simple ASP.NET MVC version 1.0 application. I have a ProductController which
I have a pretty simple ASP.NET Web Form that looks a bit like the
So I have a simple voting feature on my asp.net mvc page. My index
I have various simple ASP.NET MVC views for CRUD operations which work fine on
I have a multi-step data entry form in my ASP.NET MVC application. When the
I have VS2010, MVC3 and ASP.NET 4.0 with a simple test mvc application. The
This is an ASP.NET MVC website. Following domain driven design, we have a service

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.