Please log out of the session if the selected item is unavailable

Can someone help me figure out if what I'm attempting in asp.net is feasible? I'm encountering an issue that I could use some expertise on. :)

Within my HTML view, I am fetching values from the session and assigning them to select list items.

However, there are instances when the session data is unavailable and when a user attempts to refresh the Index page, I encounter the error:

'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'MyBranch 
.

So my objective here is to check if this returns null and if so, redirect to

Action = Logoff, Controller = Account
.

This is what I have attempted in my view:

List<SelectListItem> MyBranch = Session["MyBranch"] as List<SelectListItem>;

        if (MyBranch == null)
        {
            // I need guidance on how to redirect to this action
        }

Any suggestions or solutions?

Answer №1

Give this a shot

List<SelectListItem> BranchOptions = Session["BranchOptions"] as List<SelectListItem>;
if (BranchOptions == null) {
    return RedirectToAction("Logout", "User"); // directs to the Logout action in the User controller
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Unable to access the property 'fn' of an undefined object in electron version 2 using Angular 6

I am currently utilizing Angular 6.0.3 and electronjs 2.0.2 with the package.json configuration shown below: { "name": "test", "version": "1.0.0", "license": "MIT", "main": "electron-main.js", "author": { "name": "Moh ...

Conflicts in routing between Node.js and AngularJS

Currently, my setup involves NodeJS, gulp, and Angular with ui-router. However, I have encountered an issue when configuring Angular to remove the tag (#) from the routes. The problem arises as Angular's routes do not seem to work properly, and the na ...

What is the best way to acquire the href value from this source?

Looking to extract the dynamic value "3 Sent" from the html snippet provided. How can this be achieved? <ul class="nav nav-tabs some-tabs"> <li class="active"> <a href="#accepted" data-toggle="tab">1 Accepted</ ...

Transforming HTML 'img' elements into React components without losing styling: How do I achieve this with html-to-react?

I am seeking guidance regarding the usage of the html-to-react library. Consider the following html string: '<div> <img src="test.png" style="width: 100px;"/> <img src="test2.png" style="margin: 0px 4px;"/> </div>' ...

The enigmatic disappearance of data in PHP fopen()

My current process involves using the fopen() function with the binary-read parameter ("rb") on an image file. Here's how it looks: $imageURL = "assets/" . $gameObject . "Assets" . "/" . $gameObject . $description . "Base8SmallPNG.png"; $imag ...

Am I utilizing the htmlspecialchars function properly?

My main objective is to protect the user from malicious code and prevent XSS attacks. I have implemented a series of checks to filter the user's input before storing it in the database. The user input is stored in the $post variable. $post = htmlspec ...

Is it possible to modify only the text following the bold HTML tag?

Having trouble replacing both occurrences of "a Runner" with "a Team Captain" <form id="thisForm"> <table> <tr bgcolor="#eeeeee"> <td valign="top" colspan="4" style="vertical-align: top;"> &l ...

Adding an information panel to each column in jqgrid allows the grid to display a scrolling feature

We have implemented a jQuery grid where the last column contains a link. When this link is clicked, a new panel should appear providing additional information. To achieve this, we utilized a formatter that generates a hidden div along with the link. Howev ...

Utilize jQuery to extract all values from the second cell of a table and store them in an array

I have a task that involves pushing numbers from text boxes into an array. These text boxes are located in the second cell of each table row, while the third cell contains a date picker. Currently, the code snippet provided retrieves all values from both ...

Ensure the calling object is retained during the resolution of an Angular promise

Identifying the Issue One issue arises when resolving promises in Javascript: the context switches to Window #. This means that referring back to the object resolving the promise becomes tricky because I can't access or modify its variables. The com ...

What could be causing my function to exclude only the final element of the array when it returns?

My goal with the following code is to retrieve all items from item.children. However, I am puzzled as to why it's not returning the last item. After conducting multiple result logs and SQL verifications, I eventually pinpointed the issue in a specific ...

How to locate the position of a specific word on a webpage using jQuery

In my div, I am struggling to search like an editor. Despite multiple attempts, I have not found a solution yet. Can someone please advise me on how to resolve this issue? <div id="content"> <p> Lorem Ipsum is simply dumm ...

The integration of Zoom SDK with React and Node inevitably leads to encountering errors

I have been struggling to integrate zoomsdk into my react app and have followed all the steps mentioned here. The backend part is functioning properly, and I am receiving the signature response. However, when attempting to run the frontend portion, I encou ...

The HTML must be loaded prior to the execution of my JavaScript function

In the controller, there is a function that sets the true value to a variable. function setShow() { return this.isShow === 'Foo'; } The value of this.isShow is set to 'Foo' Within the template, there is <div ng-if = "vm.setShow( ...

The values returned by a NodeJS script in the Python shell are identical to those returned by Node

The following program was developed to address an issue in another application. In this program, a Python script is called from Node.js to perform a task. The Python script returns results that are then used in Node.js. NodeJS : var pythonShell = require ...

What is the process for including a scope in an Angular.js HTTP controller?

I am looking to access a $scope variable within this controller in order to utilize Angular functions like ng-click and ng-change in my HTML. Currently, I am only able to use the $http function but unable to execute any Angular methods on it. I am struggli ...

Error Encountered: Django - AJAX Request Not Found

I'm currently working on building an ajax function that, when triggered, should display information in a newly created modal. I seem to be encountering an error that says "not found" every time I attempt to access the specified URL. Below is a screens ...

Utilizing HTML/CSS (with Bootstrap): Reveal the concealed <input type="color"> color selector by clicking on a different HTML component

Is there a way to trigger a color picker from a hidden <input type="color"> by clicking on another HTML element, like a <span>? While this functionality seems to work on Firefox, it consistently fails on Chromium. Are there any cross- ...

Is there a way to transform the searchParams function into an object? Changing from URLSearchParams { 'title' => '1' } to { title : 1 }

Is there a way to convert the searchParams function into an object, transforming from URLSearchParams { 'title' => '1' } to { title : 1 }? I need this conversion to be applied for all values in the URLSearchParams, not just one. Cur ...

Using withRouter() to redirect will display all components

I'm brand new to Reactjs and am currently diving into routing. In my journey, I stumbled upon the use of withRouter() for programmatic redirection. I had assumed the flow would follow: constructor->Willmount->render, but it seems the current or ...