<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ASP.NET MVC</title><link>http://forums.asp.net/1146.aspx</link><description>Discussions regarding Model-View-Controller (MVC) support in ASP.NET.  &lt;a href="http://forums.asp.net/1215.aspx"&gt;T4MVC subforum&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: MasterPage&lt;TViewData&gt; Problem</title><link>http://forums.asp.net/thread/2118883.aspx</link><pubDate>Fri, 18 Jan 2008 21:33:10 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2118883</guid><dc:creator>Michael Jenkinson</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2118883.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2118883</wfw:commentRss><description>&lt;p&gt;I had trouble with rjcox&amp;#39;s solution, but I found modifying his included code to below, worked for me.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;public class&lt;/span&gt; MasterPageBugFix : System.Web.Mvc.ViewMasterPage
{
    &lt;span class="kwd"&gt;public new&lt;/span&gt; TViewData ViewData
    {
        &lt;span class="kwd"&gt;get&lt;/span&gt;
        {
            TViewData d = (TViewData)&lt;span class="kwd"&gt;this&lt;/span&gt;.ViewContext.ViewData;
            &lt;span class="kwd"&gt;if&lt;/span&gt; (&lt;span class="kwd"&gt;null&lt;/span&gt; == d)
            {
                &lt;span class="kwd"&gt;throw new&lt;/span&gt; InvalidOperationException();
            }
            &lt;span class="kwd"&gt;return&lt;/span&gt; d;
        }
    }
}&lt;/pre&gt;</description></item><item><title>Re: MasterPage&lt;TViewData&gt; Problem</title><link>http://forums.asp.net/thread/2072349.aspx</link><pubDate>Fri, 21 Dec 2007 12:21:48 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2072349</guid><dc:creator>abombss</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2072349.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2072349</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;Nice catch, thats a great interim solution.&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: MasterPage&lt;TViewData&gt; Problem</title><link>http://forums.asp.net/thread/2072339.aspx</link><pubDate>Fri, 21 Dec 2007 12:15:45 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2072339</guid><dc:creator>rjcox</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2072339.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2072339</wfw:commentRss><description>&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="/Themes/fan/images/icon-quote.gif"&gt; &lt;strong&gt;abombss:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;You think MS will give me recongnition points for this?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Maybe... but the fix does not work (unfortunetly), because ViewPage.ViewData is of type System.Web.Mvc.ViewData, which is a wrapper around the underlying _viewData field and its type has no relation to the static type parameter of MasterViewPage&amp;lt;T&amp;gt;.&lt;/p&gt;&lt;p&gt;Cast the page to an IViewDataContainer to access the underlying object, which should then be able to be cast to the required strong ViewData type.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;However a workaournd to this bug that allows use of ViewMasterPage&amp;lt;TSuperTypeOfPageViewDataType&amp;gt; should be possbile.&lt;/p&gt;&lt;p&gt;Create a class &amp;quot;MyViewMasterPage&amp;lt;T&amp;gt;&amp;quot; derived from ViewMasterPage&amp;lt;T&amp;gt;, with a single member&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;public new&lt;/span&gt; TViewData ViewData {&lt;br /&gt;  &lt;span class="kwd"&gt;get&lt;/span&gt; {&lt;br /&gt;    IViewDataContainer page = &lt;span class="kwd"&gt;this&lt;/span&gt;.Page;&lt;br /&gt;    TViewData d = page.ViewData &lt;span class="kwd"&gt;as&lt;/span&gt; TViewData;&lt;br /&gt;    &lt;span class="kwd"&gt;if&lt;/span&gt; (&lt;span class="kwd"&gt;null&lt;/span&gt; == d) {&lt;br /&gt;      &lt;span class="kwd"&gt;throw new&lt;/span&gt; InvalidOperationException(...)&lt;br /&gt;    }&lt;br /&gt;    &lt;span class="kwd"&gt;return&lt;/span&gt; d;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;</description></item><item><title>Re: MasterPage&lt;TViewData&gt; Problem</title><link>http://forums.asp.net/thread/2066941.aspx</link><pubDate>Tue, 18 Dec 2007 22:09:02 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2066941</guid><dc:creator>abombss</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2066941.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2066941</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;For sure a bug.&amp;nbsp; Here is a fix if microsoft decides to apply it.&lt;/p&gt;&lt;p&gt;Change the ViewData property of ViewMasterPage&amp;lt;TViewData&amp;gt; from:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;public&lt;/span&gt; TViewData ViewData&lt;br /&gt;{&lt;br /&gt;  &lt;span class="kwd"&gt;get&lt;/span&gt;&lt;br /&gt;  {&lt;br /&gt;    ViewPage page = &lt;span class="kwd"&gt;this&lt;/span&gt;.Page &lt;span class="kwd"&gt;as&lt;/span&gt; ViewPage&amp;lt;TViewData&amp;gt;;&lt;br /&gt;    &lt;span class="kwd"&gt;if&lt;/span&gt; (page == &lt;span class="kwd"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;      &lt;span class="kwd"&gt;throw new&lt;/span&gt; InvalidOperationException(&lt;span class="kwd"&gt;string&lt;/span&gt;.Format(CultureInfo.CurrentUICulture, AtlasWeb.ViewMasterPage_RequiresViewPageTViewData, &lt;span class="kwd"&gt;new object&lt;/span&gt;[0]));&lt;br /&gt;    }&lt;br /&gt;    &lt;span class="kwd"&gt;return&lt;/span&gt; page.ViewData;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;To&lt;br /&gt;&lt;br /&gt;&lt;span class="kwd"&gt;public&lt;/span&gt; TViewData ViewData&lt;br /&gt;{&lt;br /&gt;  &lt;span class="kwd"&gt;get&lt;/span&gt;&lt;br /&gt;  {&lt;br /&gt;    TViewData viewData = ((ViewPage)Page).ViewData &lt;span class="kwd"&gt;as&lt;/span&gt; TViewData;&lt;br /&gt;    &lt;span class="kwd"&gt;if&lt;/span&gt; (viewData == &lt;span class="kwd"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;      &lt;span class="kwd"&gt;throw new&lt;/span&gt; InvalidOperationException(&lt;span class="kwd"&gt;string&lt;/span&gt;.Format(CultureInfo.CurrentUICulture, AtlasWeb.ViewMasterPage_RequiresViewPageTViewData, &lt;span class="kwd"&gt;new object&lt;/span&gt;[0]));&lt;br /&gt;    }&lt;br /&gt;    &lt;span class="kwd"&gt;return&lt;/span&gt; viewData;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&amp;nbsp;You think MS will give me recongnition points for this?&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: MasterPage&lt;TViewData&gt; Problem</title><link>http://forums.asp.net/thread/2063839.aspx</link><pubDate>Mon, 17 Dec 2007 16:08:26 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2063839</guid><dc:creator>ian_a_anderson</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2063839.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2063839</wfw:commentRss><description>&lt;p&gt;Yeah, I realize that would be a workaround, although it&amp;#39;s a pretty significant hack.&amp;nbsp;I&amp;#39;d rather have intellisense on the ViewData for the About page (and all other pages) be generated from the custom, page-specific ViewData objects, rather than the generic ISiteMasterViewData interface. I think a better approach&amp;nbsp;would be&amp;nbsp;to add any special workaround casting to the Master page itself.&amp;nbsp;I&amp;#39;ve noticed that, in my above example, the MasterPage.ViewContext.ViewData contains the AboutViewData object while the MasterPage.ViewData property contains only the default MVC.ViewData object (which is empty). Thus, I can leave things as they are coded and manually cast the ViewContext.ViewData to ISiteMasterViewData within the master page code. Not sure why the MVC framework isn&amp;#39;t doing this itself... but it works as a workaround until I can get an answer as to whether or not this behavior is by design.&lt;/p&gt;
&lt;p&gt;-Ian&lt;/p&gt;</description></item><item><title>Re: MasterPage&lt;TViewData&gt; Problem</title><link>http://forums.asp.net/thread/2063779.aspx</link><pubDate>Mon, 17 Dec 2007 15:39:47 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2063779</guid><dc:creator>tgmdbm</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2063779.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2063779</wfw:commentRss><description>&lt;p&gt;simply define your about page as&lt;br /&gt;&amp;nbsp;&lt;br /&gt;public partial class About : ViewPage&amp;lt;ISiteMasterViewData&amp;gt;&lt;/p&gt;&lt;p&gt;and pass the data in as the interface type&lt;/p&gt;&lt;p&gt;ISiteMasterViewData data = new AboutViewData();&lt;br /&gt;RenderView(&amp;quot;About&amp;quot;, data); &lt;br /&gt;&lt;/p&gt;&lt;p&gt;obviously the names will probably change.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;br /&gt;you can also add a method to the About page to cast back to the AboutViewData type.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;br /&gt;Its not ideal tho... The master page should be able to do the cast for you.&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>MasterPage&lt;TViewData&gt; Problem</title><link>http://forums.asp.net/thread/2063453.aspx</link><pubDate>Mon, 17 Dec 2007 12:53:06 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2063453</guid><dc:creator>ian_a_anderson</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2063453.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2063453</wfw:commentRss><description>&lt;p&gt;I was hoping to be able to pass strongly-typed ViewData to master pages using an interface as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Define an Interface for the MasterPage ViewData:&lt;br /&gt;public interface ISiteMasterViewData&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Specify that the master page should require this type of ViewData:&lt;br /&gt;public partial class Site : System.Web.Mvc.ViewMasterPage&amp;lt;ISiteMasterViewData&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Define a custom ViewData class than implements the MasterPage ViewData interface:&lt;br /&gt;public class AboutViewData : ISiteMasterViewData&lt;br /&gt;&amp;nbsp; &lt;/li&gt;
&lt;li&gt;Specify my View (which is assigned to use the MasterPage for display) to use the custom ViewData class:&lt;br /&gt;public partial class About : ViewPage&amp;lt;AboutViewData&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;From the controller, construct the custom AboutViewData instance and pass it via the RenderView method:&lt;br /&gt;AboutViewData data = new AboutViewData();&lt;br /&gt;RenderView(&amp;quot;About&amp;quot;, data);&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Unfortunately, when I compile and run this setup I get the following exception:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A ViewMasterPage&amp;lt;TViewData&amp;gt; can only be used with content pages that derive from ViewPage&amp;lt;TViewData&amp;gt;.&lt;/em&gt; &lt;/p&gt;
&lt;p&gt;It seems like the MVC Framework wants to force the same explicit type for both the page as well as any masters. I would have thought that the Master page would receive the same ViewData instance that the Page receives during rendering. However, when I trace the Type of the ViewData that the Master page is receiving in the above scenario I only get the generic System.Web.Mvc.ViewData. Is this a bug? Or is there another way to pass a separate, distinct ViewData object to the Master page?&lt;/p&gt;
&lt;p&gt;-Ian&lt;br /&gt;&lt;/p&gt;</description></item></channel></rss>