开发者

Strongly typed model passed in to the view is handled as object. Why?

I am taking over quite a big ASP.NET MVC project, and I am just trying to make it work on my computer. It is asp.net MVC 1 project and I will continue developing in with Visual Studio 2010. I have set up the database and everything however I have a strange problem and no clue why it happens:

the project uses strongly typed views but somehow all the models are just handeled as plain开发者_如何学JAVA objects instead of their real types in the views (in every view). I have tried and I can cast the model to the real type (Model as RealType) and then it works, though it is ugly and I do not want to do in in 500 places in the project.... When I try to run the application the error I get is this:

CS1061: 'object' does not contain a definition for 'SomeProperty' and no extension method 'SomeProperty' accepting a first argument of type 'object' could be found

Of cource the Model actually has a property called SomeProperty that I can access after I cast it....

Anyone has any idea why is that?

Thanks a lot for any help


I ran into the same issue and while I am not sure exactly what I did to fix it, I can share the changes I made in case someone else runs into the same problem. I upgraded an MVC 1 app to MVC 3 and was experiencing the same issue as the original poster. From what I can tell, the web.config is the culprit in this situation and the changes I made are below.

Remove this from your web.config:

<httpHandlers>
  <add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpHandlers>

Remove the <handlers> section from <system.webServer>. When you're done, it should look like this:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

Add the <runtime> element after <system.webServer>:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
</runtime>


On the first line of your view make sure it is strongly typed:

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

instead of:

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

Same stands true for strongly typed partials (ascx).


After almost getting a nervous breakdown, I finally managed to make it work, thoguh I still do not understand what exactly the problem is. I write here a solution, as it might help someone: So the problem was somehow to use Visual Studio 2010 with asp.net mvc 1. By default VS2010 updates an mvc1 project to mvc 2 when you open it the first time. So first you do the obvious: change the web reference instead of the 2.0 version to 1.0 version and also in the web.config you just correct the version at the assembly section.

And here comes the big trick (without which for me the ViewPage.Model is not strongly typed but a plain object): you have to put also the following in the web.config:

<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="2.0.0.0" newVersion="1.0.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>

Yes, you say old version is 2 and new is 1 (and not just delete this section, as I did first). And this solved the problem....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜