Search code examples
c#asp.net-mvcviewbag

Why is my ViewBag not available in MVC controller?


I have an MVC website and I'm trying to set the page as active in the controller. The result I'm getting is the ViewBag is greyed out with an underline. On hover, the message says:

Unreachable code detected

Here is my home controller

enter image description here

My using statements in the controller are:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

Solution

  • You can’t have code after the return statement. That makes it... unreachable.

    Move the ViewBag assignment line to before the return View().