MVC Models

Topics: General, MVC
Jan 13, 2011 at 4:08 AM

Hi,

Does the mvc renderer function supports views with custom model?

The page works fine with :

 

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage" %>

 

and I can access the Model property in the page but when changing to:

 

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage<Company.Models.MyModel>" %>
I can not access Model property and I receive this error when rendering the page:
System.Web.HttpException (0x80004005): Could not load type 'System.Web.Mvc.ViewPage<Company.Models.MyModel>'.
   at System.Web.UI.TemplateParser.GetType(String typeName, Boolean ignoreCase, Boolean throwOnError)
   at System.Web.UI.TemplateParser.ProcessInheritsAttribute(String baseTypeName, String codeFileBaseTypeName, String src, Assembly assembly)
   at System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary parseData)

I have created a folder named Models and put it under App_Code.

I've read this article but it doesn't talk about Models.

Any idea?

 

Jan 13, 2011 at 4:38 AM

There is a workaround.

I used :

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage" %>

and inside the pages defined a variable and converted the original Model object:

<%Company.Models.MyModel model = (Company.Models.MyModel)Model; %>
    
<%=model.aProperty%>

Apparently the custom model date is there.

@core team : Any way to fix this permanently?

Jan 13, 2011 at 5:37 AM
Edited Jan 13, 2011 at 5:46 AM

Try look at this thread http://forums.asp.net/t/1193721.aspx, according to this the inherits syntax is a but cumbersome for generics. In your case it would be something like 

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage`1[[Company.Models.MyModel, WebSite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" %>

basically you have to remember that the directives in aspx/ascx pages are language-neutral and only understood by the asp.net compiler. Expecting a c#-style syntax would be unfair to the vb.net people so therefor we're defaulting to basic CLR syntax so we're not offending anyone :)

Jan 13, 2011 at 6:49 AM

Thanks for the tip. It was a pro one indeed. However I had to use the directive like this:

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage`1[[Company.Models.MyModel, App_Code.89kg8rjt, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]" %>

and this is how I found the exact name:

typeof(ViewPage<Company.Models.MyModel>).FullName

and it's a "bit" scary to have a type referenced by App_Code.89kg8rjt :)

So I decided to move my controls and models to a separate class project and now it looks like this:

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage`1[[Company.Models.MyModel, Company]]" %>

which makes more sense :)

Anyway, thank you burningice for your time.

And if anybody is reading this and misses MVC helper methods (like Html.ActionLink ) I could simply fix it by adding the namespace to web.config as below:

<pages>
.
.
.
     <namespaces>
           <add namespace="System.Web.Mvc.Html" />
      </namespaces>
</pages>

Cheers