Mr. Blond
Mr. Blond

Reputation: 1167

Remove item from session in Razor view when leaving the page

I need to clear an item from System.Web.HttpSessionStateBase.Session when navigate to any page.
This is needed as we have fields that should be cleared when click on navigate-to-page menu item.
Each item has href attribute that calls action method.

When I use sample below it shows an error- Cannot implicitly convert type void to object.

$('.navigate-to-page').on('click', function () {

   // @Session.Remove("ItemToRemoveKey");

});

Edit 1

@Hassen Ch mentioned that you cannot use razor code inside javascript, well, you acutally can!

Code that I currently have:

$('.navigate-to-page').on('click', function () {
    @{
         this.Session.Remove("ItemToRemoveKey");                 
    }
});

The issues that I have now:

1. When I navigate to other pages, no click event is fired, although when I return to previous page, it first fires the action method and the click event.
2. The action method is called first, therefore view model is not up to date.

Upvotes: 0

Views: 1445

Answers (1)

coding lion
coding lion

Reputation: 51

You can't do it client side.

You might want to define an action and call it via ajax on click

Upvotes: 1

Related Questions