I am running a site using MVC 2 and .net 4.0.
I recently added a new partial view to the site that looks like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<% if (!(bool)ViewData["Master_ShowGuide"] )
{ %>
<%= Html.NavigationButton("Guide", "button", "infoIcon", Url.Content("~/Help/ Guide.pdf"), new { title = "Click here to view an in depth user guide." }, new { target="_blank" })%>
<%} %>
What I didn’t notice at the time was the addition of the <dynamic> part to this view, where previously my partials just looked like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
This partial was displayed using:
<% Html.RenderPartial("SubLinks"); %>
on my master page.
This appeared to work ok on my development environment, but when deployed to the server, I now run into issues where my ViewData["Master_ShowGuide"] is null. I know that this is not the case, as it is used in other places on the master page just fine.
Can anyone explain why this might be the case – I have ‘fixed’ the issue by removing the <dynamic> type from the partial definition, but I’m curious as to the cause.
If it’s any help, my site runs in an environment with other sites that are running MVC 3.
As a slight addendum, I would note:
-
The user does not see this error, it just gets reported back to me (through ELMAH).
-
This code works ok on my test site, but fails with the null reference error on my pre-production deployment site (which is on the same server as the test site and currently running the same code).
I’m a little perplexed.
An answer was found to this issue (as to the root cause of why it is happening), but I’m still not sure why removing dynamic from the partial view declaration would also ‘fix’ it. If anybody can explain, I’d be interested to hear it.
Found out what the issue was, going to record it for posterity.
This wasn’t noticed on my dev system.
This resulted in a 500 server error being returned, and this seems to have disrupted the viewdata for the subsequent partial being rendered.
Hooray for firebug, as this helped me to track this down.
Hope this helps you out if you find yourself here at some point.