title: In ASP.NET MVC I encounter an incorrect type error when rendering a page with the correct typed object
body: <p>I am developing an application using MVC Preview 5.  I have used typed views.</p>
  
  <p>After setting debug="false", I have found that I am receiving errors of the form:</p>
  
  <blockquote>
    <p>"The model item passed into the
    dictionary is of type
    'blah.Models.UserAdmin.IndexData' but
    this dictionary requires a model item
    of type
    'blah.Models.OrganisationAdmin.IndexData'".</p>
  </blockquote>
  
  <p>I do not get these errors when debug is on.</p>
  
  <p>It looks like that the MVC framework is caching views by view name.  If I have two actions that use views with the same name (albeit in different namespaces), I find that only the action that is executed first is successful, the second always results in this error.</p>
  
  <p>For example, I have a UserAdminController and an OrganisationAdminController.  </p>
  
  <ul>
  <li><p>Both have an ‘Index’ action.  </p></li>
  <li><p>Both use a view called ‘Index.aspx’ (each contained in the controller’s view folder; Views/UserAdmin/Index.aspx and Views/OrganisationAdmin.Index.aspx).</p></li>
  <li><p>Both views are typed and make use of models called IndexData (blah.Models.UserAdmin.IndexData and blah.Models.OrganisationAdmin.IndexData)</p></li>
  </ul>
  
  <p>If I visit OrganisationAdmin/Index first, I find that any subsequent attempt to view UserAdmin/Index results in the error message shown above.</p>
  
  <p>Conversely, if I visit UserAdmin/Index first (after restarting the application), I find that navigating to OrganisationAdmin/Index causes an equivalent error (with the types the other way around).</p>
  
  <p>I renamed one of my views “UserAdminIndex.aspx” and this seemed to fix the problem.  However, this doesn’t feel like it should be an issue.  Surely the MVC framework support similarly named views?  I am missing something?</p>
  
  <p>Any help gratefully received.</p>
  
  <p>Sandy</p>
  
  <p>Please note, I have seen the question "In ASP.NET MVC I encounter an incorrect type error when rendering a user control with the correct typed object".  I am facing a similar problem, but I am not using RenderUserControl().</p>
  
  <p>The stack trace:</p>
  
  <pre><code>InvalidOperationException: The model item passed into the dictionary is of type 'blah.Models.RoleAdmin.IndexData' but this dictionary requires a model item of type 'blah.Models.UserAdmin.IndexData'.]
     System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +231
     System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary viewDataDictionary) +99
     System.Web.Mvc.ViewPage`1.SetViewData(ViewDataDictionary viewData) +60
     System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page) +64
     System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer) +85
     System.Web.Mvc.ViewResult.ExecuteResult(ControllerContext context) +206
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ActionResult actionResult) +19
     System.Web.Mvc.&lt;&gt;c__DisplayClass12.&lt;InvokeActionResultWithFilters&gt;b__f() +18
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +257
     System.Web.Mvc.&lt;&gt;c__DisplayClass14.&lt;InvokeActionResultWithFilters&gt;b__11() +20
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +257
     System.Web.Mvc.&lt;&gt;c__DisplayClass14.&lt;InvokeActionResultWithFilters&gt;b__11() +20
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ActionResult actionResult, IList`1 filters) +188
     System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +386
     System.Web.Mvc.Controller.ExecuteCore() +112
     System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
     System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
     System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +107
     System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +39
     System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +75
  </code></pre>