0

I have a hidden field that renders an integer siteId defined in my viewbag

Sometimes this is not present im assuming it may be related to sessions in MVC

When this happens how can I redirect to my login page? At present this is coming up with a cast error in my cshtml page

If the error happened in my controller it’s easy but I don’t know how to deal with errors in cshtml files

My cshtml has this

<div>       
    @(Html.Hidden("SiteID", (int) ViewBag.SiteID))
</div>

It appears as though if my user stays on the site for a while then tries to refresh or do something else I get cannot convert null to 'int' because it is a non nullable type

Paul

Paul
  • 2,773
  • 7
  • 41
  • 96
  • You could use the viewbag to output a redirection in javascript in your cshtml file but that sounds awful. You should make some changes so you can verify this data while still in the controller. – Phiter Nov 06 '18 at 12:26
  • I store this value in hidden field so that I can pass site id into my controller methods may be I will have to make it nullable and check if it’s null in the controller – Paul Nov 06 '18 at 12:28
  • It would be very helpful that you add some code to the post – Gonzalo Lorieto Nov 06 '18 at 12:31
  • Ok will do shortly – Paul Nov 06 '18 at 12:32
  • Not sure if I'm missing anything but in Razor you could do redirect easily? Example: https://stackoverflow.com/questions/17653736/redirecting-from-cshtml-page – JanT Nov 06 '18 at 12:32
  • How your controller looks like? – Gonzalo Lorieto Nov 06 '18 at 12:50

2 Answers2

1

As others have commented, this is not really the responsibility of the View to manage. You should have access to all the variables whilst still in your Controller level code, and you should handle it accordingly.

One way to do this would be with a Filter. In the OnActionExecuted method you could check to see if the value is populated and then redirect to the login page. Maybe something like this:

public class SiteIdFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        bool shouldRedirect = SomeMethodToCheckViewBag();
        if (shouldRedirect)
        {
             filterContext.Result = new new RedirectToRouteResult("SystemLogin", routeValues);
        }
    }
}

This answer Checking to see if ViewBag has a property or not, to conditionally inject JavaScript has and extension method for checking the view bag that looks promising

ste-fu
  • 6,879
  • 3
  • 27
  • 46
0

You could use an Actionfilter before you get to an action or after you process an action and inspect the value of the viewbag and if it's not what you want then redirect from there.

Something like this:

public class ViewBagValuePresentAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var s = filterContext.Controller.ViewBag.SiteID;
            if (s == null)
            {
                UrlHelper helper = new UrlHelper(filterContext.RequestContext);
                filterContext.Result = new RedirectResult(helper.Action("login", "Account"));
            }
            base.OnActionExecuted(filterContext);
        }

You then just put the filter attribute over the Action or controller you want it to be active on like:

[ViewBagValuePresent]
public class EditController : Controller
{}

You can read a bit more about action filters here

Topher
  • 1,011
  • 11
  • 19