There was something I thought was a bug in the previous releases - that remains in the current release - so I wanted to check you new about it - or maybe there's a nicer solution. Basically the issue is if the site's MasterPage has a type of ViewMasterPage<BaseViewData>
and you're views are type ViewPage<SomePageViewData> (where SomePageViewData inherits from BaseViewData), you get a "A ViewMasterPage<TViewData> can only be used with content pages that derive from ViewPage<TViewData>." error. I get why this is happening
- but does it not make sense to put up with a possible cast to allow for scenarios like this.
I'd be using the following to work around this:
public class MasterPageBugFix<TViewData> : System.Web.Mvc.ViewMasterPage<TViewData>
{
public new TViewData ViewData
{
get
{
TViewData d = (TViewData)this.ViewContext.ViewData;
if (null == d)
{
throw new InvalidOperationException();
}
return d;
}
}
}
and then make my MasterPage inherit from that. The only other solution I've seen is to force all the pages to inherit from the same base view data and do the casting there - which feels nasty. What do you think? Any more elegant solutions?
James Crowle...
Member
86 Points
17 Posts
MasterPage<TViewData> - where pages use a subclass of TViewData - bug fix or not?
Apr 19, 2008 01:55 PM|LINK
There was something I thought was a bug in the previous releases - that remains in the current release - so I wanted to check you new about it - or maybe there's a nicer solution. Basically the issue is if the site's MasterPage has a type of ViewMasterPage<BaseViewData> and you're views are type ViewPage<SomePageViewData> (where SomePageViewData inherits from BaseViewData), you get a "A ViewMasterPage<TViewData> can only be used with content pages that derive from ViewPage<TViewData>." error. I get why this is happening - but does it not make sense to put up with a possible cast to allow for scenarios like this.
I'd be using the following to work around this:
public class MasterPageBugFix<TViewData> : System.Web.Mvc.ViewMasterPage<TViewData>
{
public new TViewData ViewData
{
get
{
TViewData d = (TViewData)this.ViewContext.ViewData;
if (null == d)
{
throw new InvalidOperationException();
}
return d;
}
}
}
and then make my MasterPage inherit from that. The only other solution I've seen is to force all the pages to inherit from the same base view data and do the casting there - which feels nasty. What do you think? Any more elegant solutions?
Developer Fusion
James Crowle...
Member
86 Points
17 Posts
Re: MasterPage<TViewData> - where pages use a subclass of TViewData - bug fix or not?
May 17, 2008 11:19 AM|LINK
*bump*(sorry - scraop that - all ok now)
Developer Fusion