【发布时间】:2010-09-15 17:53:17
【问题描述】:
我正在使用 MVC Preview 5 开发应用程序。我使用了类型化视图。
设置 debug="false" 后,我发现我收到了表单的错误:
"模型项传入 字典是类型 'blah.Models.UserAdmin.IndexData' 但是 这本词典需要一个模型项 类型 'blah.Models.OrganisationAdmin.IndexData'”。
调试时我没有收到这些错误。
看起来 MVC 框架正在按视图名称缓存视图。如果我有两个操作使用同名的视图(尽管在不同的命名空间中),我发现只有第一个执行的操作是成功的,第二个总是导致这个错误。
例如,我有一个 UserAdminController 和一个 OrganisationAdminController。
两者都有“索引”操作。
两者都使用名为“Index.aspx”的视图(每个都包含在控制器的视图文件夹中;Views/UserAdmin/Index.aspx 和 Views/OrganisationAdmin.Index.aspx)。
两个视图都是类型化的,并使用称为 IndexData 的模型(blah.Models.UserAdmin.IndexData 和 blah.Models.OrganisationAdmin.IndexData)
如果我首先访问 OrganisationAdmin/Index,我发现任何后续尝试查看 UserAdmin/Index 都会导致上面显示的错误消息。
相反,如果我首先访问 UserAdmin/Index(在重新启动应用程序后),我发现导航到 OrganisationAdmin/Index 会导致等效错误(类型相反)。
我将我的一个视图重命名为“UserAdminIndex.aspx”,这似乎解决了问题。但是,这不应该是一个问题。 MVC 框架肯定支持类似命名的视图吗?我错过了什么?
感谢您的任何帮助。
桑迪
请注意,我看到了“在 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
【问题讨论】:
-
您找到解决方法了吗?奇怪的是,尽管我们遇到了与描述完全相同的问题,但在 MVC 3 中
标签: asp.net-mvc