加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在ASP.NET MVC中,在使用正确的类型化对象呈现页

发布时间:2020-12-16 03:42:17 所属栏目:asp.Net 来源:网络整理
导读:我正在使用MVC Preview 5开发一个应用程序.我使用了类型化的视图. 设置debug =“false”后,我发现我收到了表单错误: “The model item passed into the dictionary is of type ‘blah.Models.UserAdmin.IndexData’ but this dictionary requires a model i
我正在使用MVC Preview 5开发一个应用程序.我使用了类型化的视图.

设置debug =“false”后,我发现我收到了表单错误:

“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'”.

调试打开时,我没有收到这些错误.

看起来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,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

解决方法

是的,已报告该错误.最好暂时调试,或修改有问题的代码(抱歉不记得在哪里).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读