0

I don't know if this method I'm trying to do is possible to do in asp.net. Just wanted to ask this from experts. :)

In my HTML view, I'm reading values from the session and assigning them to the select list item.

So sometimes when the session data is out and the user tries to refresh the Index page, then I got 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'MyBranch this error.

So I'm trying to do here to check is this returns null and then I want to redirect to the Action = Logoff, Controller = Account.

In my view, this is I tried

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

        if (MyBranch == null)
        {
            // Need to know here how to redirect this to action
        }

Any ideas ?

Dev Beginner
  • 589
  • 1
  • 11
  • Does this answer your question? [With ASP.NET MVC redirect to login page when session expires](https://stackoverflow.com/questions/7091498/with-asp-net-mvc-redirect-to-login-page-when-session-expires) – Yong Shun Oct 31 '22 at 05:53
  • @SujanRai My code is in the HTML View. Not in the controller. So can't use the ``return RedirectToAction("Logoff", "Account")`` right? – Dev Beginner Oct 31 '22 at 06:10

1 Answers1

0

Try this

List<SelectListItem> MyBranch = Session["MyBranch"] as List<SelectListItem>;
if (MyBranch == null) {
    return RedirectToAction("Logoff", "Account") //this will redirect to Logoff action of Account controller
}
Sujan Rai
  • 36
  • 2
  • 7
  • My code is in the HTML View. Not in the controller. So cant use the ``return RedirectToAction("Logoff", "Account")`` right ? – Dev Beginner Oct 31 '22 at 06:09
  • You need to put this logic in controller section as it is bad practice to put logic in view page too – Sujan Rai Oct 31 '22 at 06:18