zoukankan      html  css  js  c++  java
  • ASP.NET MVC3 RenderPartial 传入空Model时出现字典类型错误

    我今天在做项目时也遇到这样的问题,在博客园中有找到合理的解释。注:这一bug在MVC4已经得到修复。不必指定最后的参数

    以下摘自:http://www.cnblogs.com/FoundationSoft/archive/2012/03/01/2376101.html

    在一个人员信息的View中,称为PersonView,调用了另外一个partial view,用来显示人员专业资质,称为QualificationView.PersonView的model类型为Person, QualificationView的model类型为IEnumerable<PersonQualification>。Person类有一个类型为List<PersonQualification>的属性qualificationList. 在PersonView中通过以下代码引用QualificaitonView。

    Html.RenderPartial("QualificationListControl",Model.qualificationList);

    当qualificationList为null时,会出现以下错误。

    “/”应用程序中的服务器错误。


    传入字典的模型项的类型为“ConstructionMis.Dll.Entity.Person”,但此字典需要类型 “System.Collections.Generic.IEnumerable`1[ConstructionMis.Dll.Entity.PersonQualification]” 的模型项。

    说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: 传入字典的模型项的类型为“ConstructionMis.Dll.Entity.Person”,但此字典需要类型 “System.Collections.Generic.IEnumerable`1[ConstructionMis.Dll.Entity.PersonQualification]” 的模型项。 源错误:

    行 96: </fieldset>
    行 97:  List<ConstructionMis.Dll.Entity.WorkExperience> workList =  ViewBag.workList; 
    行 98:  Html.RenderPartial("QualificationListControl",Model.qualificationList);
    行 99:  Html.RenderPartial("WorkExperienceControl", workList);
    行 100:}
     
    调试花了2个小时的时间,终于找到了解决方法。原来,如果在Vew A中调用了Partial View B,则给B传递model时,如果传递的参数为null,则此参数会被当作View A的model类型进行传递。即使作强制类型转换也不行。
    但是,如果不使用partial view,而是直接使用一般的view,则不会出现此问题。
    解决partial view传递null时出现类型不匹配的方法是在@Html.RenderPartial方法添加第三个参数,如下代码所示。
     Html.RenderPartial("QualificationListControl", Model.qualificationList, new ViewDataDictionary ());
    后来发现,这是ASP.NET MVC的一个bug,可以从此处查看此bug的提交报告 http://aspnet.codeplex.com/workitem/8872 关于此问题的讨论及答案可参见 http://stackoverflow.com/questions/650393/asp-net-mvc-renderpartial-with-null-model-gets-passed-the-wrong-type
  • 相关阅读:
    把office文档转换为html过程中的一些坑
    JAVA运行时问题诊断-工具应用篇
    转:IT公司的十大内耗,别说你公司没有!
    安装storm的一些很乱的笔记
    航伴项目介绍
    centos7防火墙操作
    MySQL 截取字符串
    redis客户端介绍及php客户端的下载安装
    vscode 连接远程服务器 sftp
    redis主从复制
  • 原文地址:https://www.cnblogs.com/sdikerdong/p/3149060.html
Copyright © 2011-2022 走看看