tags:

views:

38

answers:

2

Dear All,

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 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

A: 

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).

Darin Dimitrov
Yes, unfortunately it is strongly typed in the first line of the views... Any other idea??? Thanks a lot.
apolka
Where are you taken when you press F12 over the `Model` property in a View?
Darin Dimitrov
I am taken to the Model property of the ViewPage class (in System.Web.Mvc namespace) and it is indeed object....
apolka
So the question now is why your model property is of type object?
Darin Dimitrov
Yes. That is the question. But it does not go to ViewPage<TModel> but to ViewPage. Why is that??? I am really getting desperate...
apolka
Any chance of isolating the problem into a sample project which you could upload somewhere so that I can take a look at the issue because I am beginning to run out of ideas?
Darin Dimitrov
Are your models in a separate project or namespace? If so, are you using the fully-qualified name? Inherits="System.Web.Mvc.ViewPage<DomainModel.Models.RealType>
Terminal Frost
A: 

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....

apolka
That's probably not the best possible solution. Here is a link to the official document from Microsoft that explains how to do this properly: http://download.microsoft.com/download/F/1/6/F16F9AF9-8EF4-4845-BC97-639791D5699C
Robert Harvey
Would restoring your original web.config after VS has converted it to MVC2 work?
Todd Smith
Yes, I would be happy to hear a better solution. But the link you have suggested is broken somehow...
apolka
Restoring the original web.config which does not include the assemplyBinding does not work... The view models are plain objects then....
apolka