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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:16:45+00:00 2026-05-30T21:16:45+00:00

I have a login widget that is a partial view that I want updated

  • 0

I have a login widget that is a partial view that I want updated with model validation errors when there is a problem otherwise the page should be reloaded.

I have the following

LogOn.cshtml

@{
    var ajaxOpts = new AjaxOptions {OnSuccess = "success"};
}

<div id="login">
    @Html.ValidationSummary(excludePropertyErrors: true)

    <h2>Start by Logging in</h2>

    @using (Ajax.BeginForm("LogOn", "Account", ajaxOpts))
    {
        @Html.Hidden("returnUrl", Request.Url.PathAndQuery)

        <table width="100%" border="0" cellspacing="0" cellpadding="5">
            <tr>
                <td>
                    <span class="bluey">Username:</span><br />
                    @Html.TextBoxFor(m => m.UserName, new {tabindex = "1", Class = "field"})
                    @Html.ValidationMessageFor(m => m.UserName, "*")
                </td>
                <td>
                    <span class="bluey">Password:</span><br />
                    @Html.TextBoxFor(m => m.Password, new {tabindex = "2", Class = "field"})
                    @Html.ValidationMessageFor(m => m.Password, "*")
                </td>
            </tr>
            <tr>
                <td>
                    <input name="login" type="submit" value="Submit" class="input_btn" tabindex="3" />
                </td>
                <td>@Html.CheckBoxFor(m => m.RememberMe) @Html.LabelFor(m => m.RememberMe) <span class="bluey">&nbsp; | &nbsp;</span> @Html.ActionLink("Forgot Password?", "Password", "User")</td>
            </tr>
        </table>
    }
</div>

<script>
    function success(context) {
        //var returnUrl = context.get_data().returnUrl;
        //if (returnUrl) {
        //    window.location.href = returnUrl;
        //} else {
            $("#login").replaceWith(context);
        //}

    }

</script>

and the action that gets called is

    [HttpPost]
    public ActionResult LogOn(LogOnModel userDetails, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(userDetails.UserName, userDetails.Password))
            {
                FormsAuthentication.SetAuthCookie(userDetails.UserName, userDetails.RememberMe);

                return Redirect(returnUrl);
            }
            ModelState.AddModelError("", "The username or password provided was incorrect");
        }

        return PartialView(userDetails);
    }

I have it working so that when data is incorrect the partial view is redisplayed with errors however this part only works if I comment out the other lines of javascript in success(context) that are there for when the user logs in successfully and the page should be redirected.

Is there a better way to do this?

I did try returning anonymous objects with Json() that had a status property and either a returnUrl or Html however I could not figure out how to get the html that would have been generated by PartialView(userDetails) into the Json() call, and it seems like that might be the wrong way to go about things anyway.

  • 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-30T21:16:46+00:00Added an answer on May 30, 2026 at 9:16 pm

    You cannot redirect in an AJAX call. You could return a JSON object from the server pointing to the url to redirect to and then perform the actual redirect in your success callback.

    function success(result) {
        if (result.returnUrl) {
            // the controller action returned JSON
            window.location.href = returnUrl;
        } else {
            // the controller action returned a partial
            $('#login').html(result);
        }
    }
    

    Now in your controller action in case of success simply return the return url instead of redirecting:

    return Json(new { returnUrl = returnUrl });
    

    By the way you already seem to have the return url in the view: Request.Url.PathAndQuery. I don’t see the point of having it transit to the server and back. You could simply return some JSON boolean indicating the success and perform the redirect in the success callback to the url.

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

Sidebar

Related Questions

Hi I want to create a WCF service that have login method, which is
I have a Login button that I want to be disabled until 3 text
I have been trying to have a small login widget that I embed within
i have created a login page for some application in android and i want
Normally we have login page with username, password filed and signin button. Using the
on my web site I want to have login/registration form in modal window done
Basically, I currently have login/ in urls.py redirect to the django.contrib.auth.views.login and that seems
I have a login page where I authenticate the user. When the authentication pass,
I have a login screen that I force to be ssl, so like this:
I have to login into a https web page and download a file using

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.