object Model types in MVC2 using strongly typed view pages problem

醉酒当歌 提交于 2019-12-11 01:36:04

问题


I have a new converted MVC2 project running against the MVC2 source code. I have done this conversation twice on the same solution.

I use strongly typed views on every page of the site and so far I haven't had any issues running against the source nor developing with strongly typed views.

Now on one strongly typed view in particular the generic parameter is not being reflected in the Model property of that page.

Instead of having a Model of type T I always have a Model of type object.

The code for the non-working page:

<%@Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ThingViewModel>" %>
<%@ Import Namespace="SProject.Web"%>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitleContentPlaceHolder" runat="server">
    <h2>Add Encounter <%= ViewData.Model.Browser %></h2>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="SidebarContentPlaceHolder" runat="server">

The view model:

public class ThingViewModel
{
    public string Browser { get; set; }
}

No clue whats going on here.

If I add a new View using the Add View wizard everything works great but this existing page I always get an object for my view model type.

I can work around this, just wondering whats happening here?

Is something cached behind the scenes? Just curious what I'm missing.

The controller is passing a new ThingVingModel() in this case.


回答1:


You should override the parser with the new one. Check your web.config file inside the Views folder.

it should contain

<pages
    validateRequest="false"
    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <controls>
    <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
  </controls>
</pages>

instead of

 <pages
    validateRequest="false"
    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <controls>
    <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
  </controls>
</pages>


来源:https://stackoverflow.com/questions/1532838/object-model-types-in-mvc2-using-strongly-typed-view-pages-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!