传递到字典中的模型项的类型为"mvc.Models.ModelA",但此字典需要类型为"mvc.Models.ModelB"的模型项.

Mat*_*s F 70 .net c# asp.net asp.net-mvc build-process

我的一些版本中有这个恼人的错误.

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

我首先想到的可能是临时文件的问题,但事实并非如此.我将构建部署到不同的Web,但仍然出现错误.

该错误出现在网站的随机操作上.大多数时间构建都没问题,但每个第3或第4个构建都会产生运行时错误.

我在发布模式下使用WebdeploymentProject构建.视图已预编译.

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

我如何调试此问题或如何为此获得帮助?

这是我的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>
Run Code Online (Sandbox Code Playgroud)

编辑

几个月后,这个问题消失了.我现在已经有一年多没有问题了.我猜这个问题会在没人预料的情况下再次发生.

编辑2

......现在已经超过2年了.我真是个幸运老兄!

编辑3

我刚读了一篇关于MSDN的文章,听起来像是我遇到的问题.我已禁用调试,但编译仍然"有时是错误的".旧提供商的行为可能是问题.但这只是猜测.

包含长跨度HTML而没有服务器块的非常大的页面(例如<%=%>)可能会在启用调试编译时导致堆栈溢出,从而导致应用程序崩溃.请注意,在我们的测试中,它占用了大约4倍的页面,它会在旧的提供程序中产生类似的问题,但是在这种情况下它会崩溃整个应用程序,而对于旧的提供程序,它只会使违规页面失败

Mas*_*low 176

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

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

helper.RenderPartial("~/Views/Player/PlayerName.ascx", player, new ViewDataDictionary());
Run Code Online (Sandbox Code Playgroud)

作为参考,我在以下发现了这个解决方案:
使用null模型的renderpartial传递了错误的类型

  • "当将null传递给局部视图时,你可以得到这个错误" - 宾果游戏,为我修复它.谢谢! (46认同)
  • 好极了!解决了我的问题! (3认同)
  • 这里解释了为什么这样做的详细信息:http://stackoverflow.com/a/1049170/187650 (3认同)

And*_*rei 14

ASP.NET MVC Partials

NULL模型通过!!!

@Html.Partial("PartialView", model: Model)
Run Code Online (Sandbox Code Playgroud)

  • 这太烦人了,我可能只是为了抗议而迁移到LAMP堆栈! (2认同)

Nei*_* T. 7

当控制器操作方法提供给视图的数据与视图所期望的数据类型不匹配时,可能会发生此错误(并且确实存在).即使使用预编译视图,这通常也不会显示为构建错误.

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

public ActionResult Create()
{
    // Do something
    return View(new CustomerCreateViewModel());
}
Run Code Online (Sandbox Code Playgroud)

...和一个带有Page属性的Create视图......

<%@ Page ... Inherits="System.Web.Mvc.ViewPage<CustomerDetailsViewModel>" %>
Run Code Online (Sandbox Code Playgroud)

...这将编译和构建没有错误.但是,当我调用Create操作时,我将得到一个黄色屏幕,因为Create action方法抛出一种类型的数据,并且视图期望不同类型的数据.您可能想验证您的类型是否匹配...


svi*_*nto 2

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

您的依赖项设置正确吗?例如。视图取决于型号。构建通过查看指定的依赖关系来确定编译的顺序。例如。如果您更改模型并且视图在模型之前编译,您就会遇到麻烦......

如果不编译视图,这个会消失吗?

另外,您的观点是否继承System.Web.Mvc.ViewPageSystem.Web.Mvc.ViewPage<T>您的模型在哪里?