ViewBag - Object reference not found - MVC 3

ElveMagicMike picture ElveMagicMike · Feb 16, 2012 · Viewed 9.2k times · Source

Upon publishing my MVC 3 Web Application to my website I get an error stating an Object reference not set to an instance of an object.

The 'error' line is: Line 2: ViewBag.Title = "Index";

This is my Index view:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

This is my Home Controller:

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

namespace WebApplication.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

    }
}

I have uploaded DLL's:

System.Web.Helpers
System.Web.Mvc
System.Web.Razor
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor

Thanks for any assistance

Answer

Serdar picture Serdar · Apr 26, 2013

I hit the same ViewBag error. in my case Model was null but the exception was directing to viewbag row.

When I passed a model to view error fixed.