我正在使用 MVC Preview 5 开发一个应用程序。我使用了类型化视图。
设置 debug="false" 后,我发现收到以下形式的错误:
“模型项目传递到
字典的类型是
'blah.Models.UserAdmin.IndexData' 但是
这本词典需要一个模型项目
类型的
'blah.Models.OrganizationAdmin.IndexData'”。
当调试打开时,我没有收到这些错误。
看起来 MVC 框架正在按视图名称缓存视图。如果我有两个使用同名视图的操作(尽管在不同的命名空间中),我发现只有第一个执行的操作才会成功,第二个操作总是会导致此错误。
例如,我有一个 UserAdminController 和 OrganizationAdminController。
两者都有一个“索引”操作。
两者都使用名为“Index.aspx”的视图(每个视图都包含在控制器的视图文件夹中;Views/UserAdmin/Index.aspx 和 Views/OrganizationAdmin.Index.aspx)。
这两个视图都是类型化的,并使用称为 IndexData 的模型(blah.Models.UserAdmin.IndexData 和 blah.Models.OrganizationAdmin.IndexData)
如果我首先访问 OrganizationAdmin/Index,我发现任何随后尝试查看 UserAdmin/Index 的结果都会出现上面显示的错误消息。
相反,如果我首先访问 UserAdmin/Index(重新启动应用程序后),我发现导航到 OrganizationAdmin/Index 会导致等效错误(类型相反)。
我将我的视图之一重命名为“UserAdminIndex.aspx”,这似乎解决了问题。然而,这并不像是一个问题。 MVC 框架肯定支持类似命名的视图吗?我缺少什么吗?
非常感谢任何帮助。
Sandy
请注意,我看到过这样的问题“在 ASP.NET MVC 中,当使用正确类型的对象渲染用户控件时,我遇到了不正确的类型错误”。我面临类似的问题,但我没有使用 RenderUserControl()。
堆栈跟踪:
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