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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:20:08+00:00 2026-06-11T22:20:08+00:00

I want to call OnFailure when ModelState is not valid in controller. In My

  • 0

I want to call “OnFailure” when ModelState is not valid in controller.

In My LoginView

 @using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "Login",InsertionMode = InsertionMode.Replace, OnSuccess = "Success", OnFailure = "onError" }))
 {

 } 

In Controller

[httpPost]
public ViewResult Login(LoginModel model)
{
   if (ModelState.IsValid)
   {

   }
   else
   { 
     ModelState.AddModelError("login is fail")
   }
   return View("Login",model)
}

so i want call onSuccess Method if ModelState is valid and if it fail then call only OnError method with display all error which are in Model State.

  • 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-11T22:20:09+00:00Added an answer on June 11, 2026 at 10:20 pm

    Here’s what you could do:

    [HttpPost]
    public ActionResult Login(LoginModel model)
    {
        if (ModelState.IsValid)
        {
            // everything went fine and we want to redirect in this case =>
            // we pass the url we want to redirect to as a JSON object:
            return Json(new { redirectTo = Url.Action("SomeController", "SomeAction") });
        }
        else
        { 
            // there was an error => add an error message
            ModelState.AddModelError("login is fail")
        }
    
        // return a partial view instead of a full vire
        return PartialView("Login",model)
    }
    

    and then all you need is the Success function:

    @using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", OnSuccess = "loginAjaxSuccess" }))
    {
    
    } 
    

    in which you could test in which case you are:

    function loginAjaxSuccess(result) {
        if (result.redirectTo) {
            // the controller action returned a JSON result => it was a successful login
            // => we redirect the browser to this url
            window.location.href = result.redirectTo;
        } else {
            // the action returned a partial view with the form containing the errors
            // => we need to update the DOM:
            $('#Login').html(result);
        }
    }
    

    By the way if you are using unobtrusive client side validation in the case of error where you are refreshing the form you will need to manually force the parsing of the new validation rules, otherwise next time you attempt to submit the form, client validation won’t work:

    } else {
        // the action returned a partial view with the form containing the errors
        // => we need to update the DOM
        $('#Login').html(result);
    
        // Now that the DOM is updated let's refresh the unobtrusive validation rules on the form:
        $('form').removeData('validator');
        $('form').removeData('unobtrusiveValidation');
        $.validator.unobtrusive.parse('form');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call a webservice using google closures, via jsonp since i am
i want to call the command prompt command using Process.Start and then using StandardOutput
I want to call a website using PL/SQL and get the return value of
I want to call browser window resize method using jquery from with in iframe.
I want call a function which makes my main thread stuck using the above
I want to call my controller when users click backward and forward. But I
I want to call an https url from java and it is not trusted,
I want to call a html page on click of a button in mvc.
I want to call a method say success(message) of javascript from android activity. I
I want to call a web service that requires an authentication cookie. I have

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.