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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:54:02+00:00 2026-06-05T08:54:02+00:00

I have a section in my ASP.NET MVC3 website where a user can click

  • 0

I have a section in my ASP.NET MVC3 website where a user can click a button to add an entry to their ‘Saved Items’ section in their account. This is done via a JQuery Ajax request, which works well if they’re logged in. If they’re not logged in, I’d like them to be redirected to a login page, and then automatically have the entry added to their Saved Items section.

I have all the parts working seperately – i.e. when the button is clicked, if not logged in, the login box displays. The login popup also works successfully. The problem is trying to seamlessly do all things at once. Here is the code I have so far:

Click event for Save button – checks to see if user logged in along the way:

    var loggedIn = false;

    $(document).ready(function () {
        $('a#saveSearch').live('click', function (event) {
            $.get('@Url.Action("IsLoggedIn", "Account", null)', function (response) {
                if (response == "True")
                    loggedIn = true;
                else
                    loggedIn = false;
            });
            if (loggedIn){
                SaveSearch();
            }
            else{                        
                $('#dialog').dialog('open');
                SaveSearch(); //don't think this is correct because it hits this line before login is complete
            }
        });

Function to save to database:

        function SaveSearch(){                    
            var url = '@Url.Action("SaveSearch", "User")';
            $.ajax({
                url: url,
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({
                    json: "@Html.Raw(Session["MyFormString"].ToString())"
                }),
                success: function (data) {
                    $('a#saveSearch').attr('disabled', "disabled");
                    $('div#savedResponse').html('<p>Search saved to user account</p>');
                },
                error: function () {
                }
            });
        }
    });

JQuery UI dialog popup:

$(function () {
    $('#dialog').dialog({
        autoOpen: false,
        width: 400,
        resizable: false,
        title: 'Login',
        modal: true,
        open: function(event, ui) {
            $(this).load("@Url.Action("Logon", "Account", null)");
        },
        buttons: {
            "Close": function () {
                $(this).dialog("close");
            }
        }
    });

I think there is something fundamental that is wrong with my code, because this way, the login popup appears for just a second and then disappears straight away. It looks like I need to get it to stop advancing through the code until the login has been completed.

Any advice or help to get this going would be appreciated.

  • 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-05T08:54:04+00:00Added an answer on June 5, 2026 at 8:54 am

    I would imagine your issue might be related to:

    $.get('@Url.Action("IsLoggedIn", "Account", null)', function (response) {
      if (response == "True")
        loggedIn = true;
      else
        loggedIn = false;
    });
    if (loggedIn){
      SaveSearch();
    }
    else{                        
      $('#dialog').dialog('open');
      SaveSearch(); //don't think this is correct because it hits this line before login is complete
    }
    

    The $.get call is async, which means the latter code:

    if (loggedIn){
    

    Is being executed before the server has responded. You need to put that code within your response callback:

    $.get('@Url.Action("IsLoggedIn", "Account", null)', function (response) {
      if (response == "True")
        loggedIn = true;
      else
        loggedIn = false;
    
      if (loggedIn){
        SaveSearch();
      }
      else{                        
        $('#dialog').dialog('open');
        SaveSearch(); //don't think this is correct because it hits this line before login is complete
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using asp.net mvc and have a section where a user can upload
I have a section of my site where users can add addresses to their
I have a section on a page where a user can change their username.
In my ASP.NET MVC 3 application I have a section to add an event
I have a asp.net MVC3 Application with two users Admin and General.Cookies is saved
Can somebody tell me how EF/asp.net caches db entries? I have a simple mvc3
I have a asp.net application.there is a databinding section to a datagrid.But there have
I have an ASP.NET application with a <codeSubDirectories> section in web.config. My main project
I have a Site.Master in my ASP.NET project which defines a HEAD section as
I have published an ASP.NET MVC3 site. It runs great. However, looking back at

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.