ElveMagicMike
ElveMagicMike

Reputation: 175

ViewBag - Object reference not found - MVC 3

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

Upvotes: 4

Views: 10338

Answers (3)

Ziaullah Khan
Ziaullah Khan

Reputation: 2246

please ensure that layout (_Layout.cshtml) is present inside Views folder. cshtmls/aspx inside the Views folder only inherit the ViewBag property.

~ Z

Upvotes: 0

Serdar
Serdar

Reputation: 1516

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.

Upvotes: 14

Kristof Claes
Kristof Claes

Reputation: 10941

When you look in /Views/Shared/_Layout.cshtml, you will probably see something like this:

<title>@ViewBag.Title</title>

It is assuming this property has been set somewhere. Usually, you set this in your child views. So the easiest solution is to modify your Index.cshtml view so that it resembles something like this:

@{
     ViewBag.Title = "Title of the page";
     Layout = "~/Views/Shared/_Layout.cshtml";
}

Upvotes: 5

Related Questions