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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:58:53+00:00 2026-06-14T15:58:53+00:00

I am building an website in asp .net mvc 3. I am trying to

  • 0

I am building an website in asp .net mvc 3.

I am trying to create a simple toggle button which can be used to “Add to favorite” and
“Remove from favorite”. However, I only want this functionality if the user is logged in
otherwise I want to direct him to the “Login” page.

The toggle button works well but it does not check if the user is logged in or not. If a
user is not logged in then on clicking the button it toggles but does not update the
database. I want it to direct to the login page.

My code is below:

View:

<div class="save-unsave-link">
    @if(Model.IsPropertySaved) {
        @Html.ActionLink("Remove Property", "RemoveSavedProperty", "Property", new { id = Model.Property.PropertyId }, new { @class="unsave-property", onclick = "saveProperty();" })      
    } else {
        @Html.ActionLink("Save Property", "AddSavedProperty", "Property", new { id = Model.Property.PropertyId }, new { @class="save-property", onclick = "saveProperty();" })
    }
</div>

jQuery:

function saveProperty() {
    $('.save-unsave-link').delegate("a", "click", function (e) {
        var id = $(this).attr('href').match(/\d+/);
        if ($(this).hasClass('unsave-property')) {
            $.ajax({
                url: this.href,
                dataType: "text json",
                type: "POST",
                data: {},
                success: function (data, textStatus) { }
            });
            $(this).removeClass().addClass("save-property")
                .attr('href', '/Property/RemoveSavedProperty/' + id)
                .text('Remove Property');
            e.preventDefault();
        } else {
            var id = $(this).attr('href').match(/\d+/);
            $.ajax({
                url: this.href,
                dataType: "text json",
                type: "POST",
                data: {},
                success: function (data, textStatus) { }
            });
            $(this).removeClass().addClass("unsave-property")
                .attr('href', '/Property/AddSavedProperty/' + id)
                .text('Save Property');
            e.preventDefault();
        }
    });
}

Controller:

//
// POST: /Property/AddSavedProperty
[HttpPost]
[Authorize]
public void AddSavedProperty(int id)
{
    websiteRepository.AddSavedProperty(id);
}

//
// POST: /Property/RemoveSavedProperty
[HttpPost]
[Authorize]
public void RemoveSavedProperty(int id)
{
    websiteRepository.RemoveSavedProperty(id);
}

How do I check if the user is logged in before ajax post? and if he is not logged in then
how do I direct him to the login page?

  • 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-14T15:58:54+00:00Added an answer on June 14, 2026 at 3:58 pm

    Why not just render out a link to your Login action if the user isn’t logged in? You don’t need jQuery for this at all – the Ajax call is completely superfluous when you can already determine whether the user is logged in when you first render the page.

    @if (User.Identity.IsAuthenticated)
    {
        @Html.ActionLink("Save Property", "AddSavedProperty", "Property", new { id = Model.Property.PropertyId },
            new { @class="save-property", onclick = "saveProperty();" })
    }
    else
    {
        @Html.ActionLink("Save Property", "Login", "Login",
            new { returnUrl = ViewContext.HttpContext.Request.Url.PathAndQuery }, null)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building my first ASP.NET MVC website, and I'm trying to figure out how
I am currently building a website in ASP.NET MVC. I am trying to access
I am building an asp.net mvc website, after the user login he can access
I am building a calendar website ( ASP.NET MVC ) application (think simple version
I've been developing a simple website using asp.net MVC and I'm starting to add
I am building a web site in C# using asp.NET MVC How can I
I am building an ASP.NET website which will collect data from a user and
I'm building a website with ASP.NET MVC where one feature displays some points on
Building a ASP.NET MVC site and I would like to add some values to
I'm building an asp.net MVC application where users can attach a picture to their

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.