传递到字典中的模型项目types为“mvc.Models.ModelA”,但是此字典需要“mvc.Models.ModelB”types的模型项目

在我的一些版本中,我有这个恼人的错误。

项目中没有错误,因为如果我再次构build,那么问题就会消失。 该消息仅在网站部署到Windows 2008 Server时才会显示。

我首先想到这可能是临时文件的问题,但事实并非如此。 我将这个版本部署到了一个不同的网站,并且仍然出现错误。

该错误出现在网站的随机操作上。 大部分时间构build都可以,但是每个第三或第四个构build都会产生运行时错误。

我在发布模式下使用WebdeploymentProject构build。 视图是预编译的。

这不是在ASP.NET MVC中,当使用正确的types对象呈现页面时,我遇到了错误的types错误 ,因为视图具有完全不同的名称。

我怎样才能debugging这个问题,或者我可以如何得到这个帮助?

这是我的WebDeploymentProject

<!-- Microsoft Visual Studio 2008 Web Deployment Project http://go.microsoft.com/fwlink/?LinkID=104956 --> <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProductVersion>9.0.21022</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{E5E14CEB-0BCD-4203-9A5A-34ABA9C717EA}</ProjectGuid> <SourceWebPhysicalPath>..\B2CWeb</SourceWebPhysicalPath> <SourceWebProject>{3E632DB6-6DB3-4BD0-8CCA-12DE67165B48}|B2CWeb\B2CWeb.csproj</SourceWebProject> <SourceWebVirtualPath>/B2CWeb.csproj</SourceWebVirtualPath> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <OutputPath>.\Debug</OutputPath> <EnableUpdateable>false</EnableUpdateable> <UseMerge>true</UseMerge> <SingleAssemblyName>B2CWeb_Build</SingleAssemblyName> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugSymbols>false</DebugSymbols> <OutputPath>..\B2CWeb_Deploy\</OutputPath> <EnableUpdateable>false</EnableUpdateable> <UseMerge>true</UseMerge> <SingleAssemblyName>B2C_Web</SingleAssemblyName> <ContentAssemblyName> </ContentAssemblyName> <DeleteAppCodeCompiledFiles>false</DeleteAppCodeCompiledFiles> </PropertyGroup> <ItemGroup> </ItemGroup> <Import Project="$(MSBuildExtensionsPath)\Microsoft\WebDeployment\v9.0\Microsoft.WebDeployment.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.WebDeployment.targets. <Target Name="BeforeBuild"> </Target> <Target Name="BeforeMerge"> </Target> <Target Name="AfterMerge"> </Target> <Target Name="AfterBuild"> </Target> --> </Project> 

编辑

几个月后,这个问题消失了。 现在我已经有一年多没有问题了。 我想这个问题在没有人期待的时候会再次出现。

编辑2

…超过2年了。 我是一个幸运的家伙!

编辑3

我只是读了一篇关于MSDN的文章 ,听起来像是我遇到的问题。 我禁用了debuggingfunction,但仍然是编译“有时是错误的”。 旧供应商的行为可能是问题。 但这只是猜测。

当debugging编译被启用会导致应用程序崩溃时,非常大的页面包含长时间跨度的HTML而没有服务器块(例如<%=%>)可能导致堆栈溢出。 请注意,在我们的testing中,这个页面的大小约为旧的提供程序的四分之一,但是在这种情况下,它会使整个应用程序崩溃,而使用旧的提供程序则只会使违规页面失效

即使types匹配,当null传递给局部视图时,也可能会出现此错误。

你可以通过像下面这样用一个空的ViewDataDictionary来调用RenderPartial来解决这个问题:

 helper.RenderPartial("~/Views/Player/PlayerName.ascx", player, new ViewDataDictionary()); 

作为参考,我发现这个解决scheme在:
渲染部分与空模型获取通过错误的types

ASP.NET MVC的部分

NULL模型通过了!

 @Html.Partial("PartialView", model: Model) 

如果控制器操作方法向视图提供的数据与视图所期望的数据types之间存在不匹配,则可能会发生此错误(并且会发生此错误)。 这通常不会显示为构build错误,即使预编译视图也是如此。

例如,如果我有一个方法…

 public ActionResult Create() { // Do something return View(new CustomerCreateViewModel()); } 

…以及使用页面属性创build视图…

 <%@ Page ... Inherits="System.Web.Mvc.ViewPage<CustomerDetailsViewModel>" %> 

…这将编译和build立没有错误。 但是,当我调用创build操作时,我将会看到一个黄色的屏幕,因为Create操作方法正在抛出一种types的数据,并且该视图期待不同types的数据。 你可能想validation你的types是否匹配…

你是否确定这与传递给视图的数据无关? 你每次都在进行完整的重build吗?

通常会发生这些错误,因为当传递给部分视图的视图模型为空时,部分视图尝试使用传递给ViewPage的视图模型。 我意识到你是暗示错误是由构build过程造成的,但是我不明白这是怎么可能的。 难道部署的网站使用的数据库与您在开发计算机上运行的网站不同,数据库中的数据(或缺less数据)是否会成为问题的原因?

构build是否会触发任何警告或错误?

你有你的依赖设置正确吗? 例如。 视模型而定。 通过查看指定的依赖关系,以什么样的顺序构build数字。 例如。 如果您更改模型,并且在模型之前编译视图,您将遇到麻烦…

如果你不编译视图,这消失了吗?

也做你的意见inheritanceSystem.Web.Mvc.ViewPageSystem.Web.Mvc.ViewPage<T>其中T是你的模型?

在将模型分配给您的视图中的usercontrol之前,在模型/实体的构造函数中实例化该特定对象。

例:

  public class MainEntity { public SubEntity AssociatedEntity; public MainEntity() { // This is where the instantiation happen. AssociatedEntity = new SubEntity(); } } public class SubEntity { public string property1; } 

您的查看页面:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyNamespace.Models.MainEntity>" %> .... <%Html.RenderPartial("ucMyUserControl",Model.AssociatedEntity); .... 

您的用户控制:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Models.SubEntity>" %> .... <%Html.TextBoxFor(m=>m.Property1); 

如果您有一个重写的Views文件夹,其中存在多个同名部分(一个重写另一个具有相同名称),请确保在每个视图上更新模型types,否则可能会收到此错误。

我有以下错误信息

传入字典的模型项目types为“System.Linq.Enumerable + WhereSelectListIterator2 [MyProject.Models.A,MyProject.Models.B]”,但是此字典需要types为“System.Collections.Generic.List1 [MyProject.Models.B]”。

用下面的代码

 IEnumerable<B> list_B; ... IEnumerable<A> list_A = Repo.GetListOfType_A(); list_B = list_A.Select(x => new B { Number = x.Number, Name = x.Name }); ... return View(list_B); 

我不知道是什么types

System.Linq.Enumerable + WhereSelectListIterator2 [MyProject.Models.A,MyProject.Models.B]

无论如何,当我在最后添加一个.ToList()时,错误消失

 IEnumerable<B> list_B; ... IEnumerable<A>list_A = Repo.GetListOfType_A(); list_B = list_A.Select(x => new B { Number = x.Number, Name = x.Name }).ToList(); // here ... return View(list_B); 

希望它可以帮助别人