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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:38:36+00:00 2026-06-15T05:38:36+00:00

@using (Ajax.BeginForm(Login, Account, , new AjaxOptions { HttpMethod = POST }, new { id

  • 0
@using (Ajax.BeginForm("Login", "Account", "", 
            new AjaxOptions { HttpMethod = "POST" }, 
            new { id = "loginForm", name = "loginForm" }))
{
 ...
}

This form perform a request and receive a response 200 OK. Debbuging I can see the response html but I don´t get redirected.

If I do it manually without using html helps I get successfully redirected to where I need to.

This is the controller:

//
    // POST: /Account/Login
    [HttpPost]        
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model)
    {
        MembershipProvider mp = Membership.Provider;
        bool isDigital = bool.Parse(Request.Form.GetValues("hasDigital")[0]);
        string certDBNumber;

        if (isDigital)
        {
            /*** Retira só o que enteressa do Certificado.Subject (CPF/CNPJ)*/
            string code = Request.Form.GetValues("code")[0];
            string[] dataArray = code.Split(',');
            string data = dataArray.Last();
            string[] numberArr = data.Split(':');
            string number = numberArr.Last();

            /*** Resgata chave do usuário no banco ***/
            using (tgpwebgedEntities context = new tgpwebgedEntities())
            {
                var userObj = from u in context.aspnet_Users 
                              where u.UserName == model.UserName 
                              select u.UserId;
                Guid userID = userObj.First();
                var obj = from u in context.sistema_UsersCertified 
                          where u.userID == userID select u.keyNumber;
                certDBNumber = obj.First();
            }

            //Verifica se usuário é credenciado
            if (number == certDBNumber) {
                FormsAuthentication.SetAuthCookie(model.UserName, false);
                return RedirectToAction("Index", "Home");                    
            }
        }//Login sem certificado digital
        else
        {
            if (ModelState.IsValid && 
                           mp.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false);
                return RedirectToAction("Index", "Home");
            }
        }
        /*** Se chegou até aqui algo deu errado. Mostra denovo o form ***/
        ModelState.AddModelError("", "Username ou Password incorreto!.");
        return View(model);
    }

Why this strange behavior?

  • 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-15T05:38:37+00:00Added an answer on June 15, 2026 at 5:38 am

    As a result of this being an ajax post, you cannot redirect. An option would be to return a value such that the success function realized the redirection needed to take place, and then issued

    if( someReturnedFlag == true ){
     window.location = redirectionUrl;//http://www.stackoverflow.com
    }
    

    Alternatively, you could make a redirection view

    RedirectionView.cshtml

    @{
     Layout = null;
    }
    <script type="text/javascript">
     window.location = "hardcodedurl";//or pass it in using @model
    </script>
    

    And then return this view from your ajax post and it would redirect.


    Edit

    This got more attention than I expected, so I thought I would improve this answer a little bit with two more complete examples.


    1: jQuery’s ajax

    view:

    $.ajax({
            url: "@(Url.Action("TargetAction","TargetController"))",
            type: 'POST',
            data: $("#loginForm").serialize(),
            success: function (URL) {
             window.location = URL;
            }
    });
    

    controller:

    public class TargetController: Controller
    [HttpPost]
    public string TargetAction(ViewModel model)
    {
     //use model (note that the serialized form was bound to the model)
     return "http://www.stackoverflow.com";
    }
    

    2: RedirectionView.cshtml

    main view:

    @{
     AjaxOptions ajaxOpts = new AjaxOptions
     {
        UpdateTargetId = "redirectWhenDone"
     };
    }
    
    @using (Ajax.BeginForm("TargetAction", ajaxOpts))
    {
     ...
    }
    <div id="redirectWhenDone"></div>
    

    RedirectionView.cshtml

    @model string
    @{
     Layout = null;
    }
    <script type="text/javascript">
     window.location = "@(Model)";
    </script>
    

    Controller

    [HttpPost]
    public ActionResult TargetAction(ViewModel vm)
    {
     //use ViewModel
    
     ...
    
    
     //it is important to use an object here for the string
     object url = "http://www.stackoverflow.com";
     //otherwise the View() method will consider the string a parent location and look
     //for the RedirectionView in the wrong place (basically using the wrong overload)
     return View("RedirectionView", url );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a loginform like this: @using (Ajax.BeginForm(Login, new AjaxOptions() { HttpMethod =
From this call @using (Ajax.BeginForm(Manage, Account, new AjaxOptions { HttpMethod = POST, Confirm =
I have a page as follows: <% using (Ajax.BeginForm(AddAnswer,Marketplace,new AjaxOptions() {HttpMethod = POST })){
This is my Test.cshtml: @using(Ajax.BeginForm(Test, new AjaxOptions())) { <p> Some String: <input name=someString type=text
@using (Ajax.BeginForm(Edit, xyz, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = POST, UpdateTargetId =
I am using Ajax.BeginForm like this: @using (Ajax.BeginForm(PostAction, null, new AjaxOptions { HttpMethod =
I have a form created using Ajax.BeginForm() <% using (Ajax.BeginForm(UpdateCompanyShop, CompanyShop, FormMethod.Post, new AjaxOptions
I have an Ajax Form that looks like this: <% using (Ajax.BeginForm(Update, new AjaxOptions
Is it possible to do something like this using asp.net mvc 3 @using(Ajax.BeginForm(SomeAction,MyController,new AjaxOptions
i have view which looks something like this : <% using (Ajax.BeginForm(new AjaxOptions {

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.