tags:

views:

971

answers:

3

Please can anybody point out where this code might be going wrong. I am trying to create a partial class for the masterpage.

The master pages class:

namespace MuniWeb.Models.SiteMaster{

public class BaseViewData
{
    public string Title { get; set; }
    public string MetaKeywords { get; set; }
    public string MetaDescription { get; set; }
}

public partial class Site : System.Web.Mvc.ViewMasterPage<MuniWeb.Models.SiteMaster.BaseViewData>
{
    public Site()
    {
        ViewData.Model = new BaseViewData();
    }
}}

The master page:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MuniWeb.Models.SiteMaster.BaseViewData>" %>

The error:

Object reference not set to an instance of an object.
Line 33:             <div id="footer">
Line 34:                 ApplicationID:
Line 35:                 <%= Model.Title %>
Line 36:             </div>
Line 37:         </div>

I am just using this as an example. The code I want to use needs to fire for every page to check certain parameters, this is why it is in the master page.

A: 

You shouldn't need to make a partial class. What does your controllers action code look like? The error looks like it could be from not handing the View (and therefore it's master page) a model.

Try something like this:

namespace MuniWeb.Website.ViewDataModels {

    public class BaseViewData
    {
        public string Title { get; set; }
        public string MetaKeywords { get; set; }
        public string MetaDescription { get; set; }
    }

    public class SubViewData : BaseViewData
    {
        public IList<Thing> Things { get; set; }
    }
}

Then define your master page like you had:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MuniWeb.Website.ViewDataModels.BaseViewData>" %>

Now in your controller

public ActionResult Index()
{
    SubViewData viewData = new SubViewData();
    viewData.Title = "Page Title";
    viewData.MetaKeywords = "This, that, and the other";
    viewData.MetaDescription = "A really great page about this, that, and the other.";

    viewData.Things = _myRepository.GetThings();

    return View(viewData);
}

See how that goes...

I would ask why the MasterPage NEEDS strongly type ViewData. I understand that yes, sometimes strongly typed viewdata is needed in masterpages but generally you should be able to get way with just using the ViewData name value collection.

Charlino
A: 

Shouldn't your master page be inheriting Site and not System.Web.Mvc.ViewMasterPage? No where does your master page definition actually reference the Site class.

andymeadows
A: 

In your code,

public partial class Site :  
         System.Web.Mvc.ViewMasterPage<MuniWeb.Models.SiteMaster.BaseViewData>
{
    public Site()
    {
        ViewData.Model = new BaseViewData();
    }
}}

ViewData.Model = new BaseViewData(); is not necessary. The "BaseViewData" should be passed in via the controller. In other words, all of your views should take look for a View that inherits the base view. The Master Page will have that same object cast as the base class BaseViewData. Your controller code appears to be correct in doing just that.

Another thing that appears to be different from my similar code is Inherits="System.Web.Mvc.ViewMasterPage" should be Inherits="my.codebehind.class", then your codebehind would inherit ViewMasterPage. I've just not seen the code as such, perhaps it works?

Greg Ogle