6
votes

I am developing an application using MVC Preview 5. I have used typed views.

After setting debug="false", I have found that I am receiving errors of the form:

"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'".

I do not get these errors when debug is on.

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.

For example, I have a UserAdminController and an OrganisationAdminController.

  • Both have an ‘Index’ action.

  • Both use a view called ‘Index.aspx’ (each contained in the controller’s view folder; Views/UserAdmin/Index.aspx and Views/OrganisationAdmin.Index.aspx).

  • Both views are typed and make use of models called IndexData (blah.Models.UserAdmin.IndexData and blah.Models.OrganisationAdmin.IndexData)

If I visit OrganisationAdmin/Index first, I find that any subsequent attempt to view UserAdmin/Index results in the error message shown above.

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).

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?

Any help gratefully received.

Sandy

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().

The stack trace:

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.<>c__DisplayClass12.<InvokeActionResultWithFilters>b__f() +18
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +257
   System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +257
   System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>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& completedSynchronously) +75
3
Did you ever find a fix for this? Bizarrely despite the age of the question we're hitting the exact same problem as described, but in MVC 3Alex Norcliffe

3 Answers

2
votes

This error was driving me nuts. The Model was being specified at the top of my view. Yet the damn thing was requesting a different model.

Turns out there was an orphan ModelType reference in what had become a Layout page. It was being referenced in my view as Layout = "~/Views/_layout.vbhtml"

1
votes

Yes, the bug has been reported. Best leave debug on for now, or modify the offending code (sorry cant recall where).

1
votes

I Think your usercontrol and view have same name.