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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:41:09+00:00 2026-05-22T18:41:09+00:00

I want to post a form via ajax call also model will be passed

  • 0

I want to post a form via ajax call also model will be passed into the action method, but want to get Model errors via json. How can I do this?

  • 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-22T18:41:09+00:00Added an answer on May 22, 2026 at 6:41 pm

    You could write a custom action filter:

    public class HandleJsonErrors : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var modelState = (filterContext.Controller as Controller).ModelState;
            if (!modelState.IsValid)
            {
                // if the model is not valid prepare some JSON response
                // including the modelstate errors and cancel the execution
                // of the action.
                // TODO: This JSON could be further flattened/simplified
                var errors = modelState
                    .Where(x => x.Value.Errors.Count > 0)
                    .Select(x => new
                    {
                        x.Key,
                        x.Value.Errors
                    });
                filterContext.Result = new JsonResult
                {
                    Data = new { isvalid = false, errors = errors }
                };
            }
        }
    }
    

    and then put it into action.

    Model:

    public class MyViewModel
    {
        [StringLength(10, MinimumLength = 5)]
        public string Foo { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [HttpPost]
        [HandleJsonErrors]
        public ActionResult Index(MyViewModel model)
        {
            // if you get this far the model was valid => process it
            // and return some result
            return Json(new { isvalid = true, success = "ok" });
        }
    }
    

    and finally the request:

    $.ajax({
        url: '@Url.Action("Index")',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({
            foo: 'abc'
        }),
        success: function (result) {
            if (!result.isvalid) {
                alert(result.errors[0].Errors[0].ErrorMessage);
            } else {
                alert(result.success);
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an action handling a form post, but I want to make sure
I post via Ajax and get a return value which I then want to
I have the following form that has 2 selects: <form id=form class=form_visitar method=post action=ajax/selects.php>
I have login form which i want to send via ajax using serialize but
Im opening a form via ajax (using jquery) and i want to save it
I am using Jquery AJAX to post a form on my page. I want
I want to ignore the post form in the django's internatonalization. I am using
I want to make a form like this, and i want to post the
I have a nested json. I want to post it as a form input
I want to do an HTTP POST that looks like an HMTL form posted

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.