We're using (there is need to tell where the files are, thanks) custom T4 code templates on creating a view or controller. Default implementation makes this kind first row.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProjectNameHere.ViewModels.ViewModelClass>" %>
We have already ViewModel and MVC namespace defined in the Web.config, so I would like code template to generate this.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ViewModelClass>" %>
Any suggestions how modify the default templates to get that kind of results? Which of the template lines actually generate these?
More information
I know where the files are and modifications has been made. Problem is that in the template they're using this
string mvcViewDataTypeGenericString = (!String.IsNullOrEmpty(mvcHost.ViewDataTypeName)) ? "<" + mvcHost.ViewDataTypeName + ">" : String.Empty;
It seems that ViewDataTypeName contains full namespace reference. I would like get just name of the class (in this case ViewModel class name)
You can find the templates here:
*\Microsoft Visual Studio \Common7\IDE\ItemTemplates\CSharp\Web\MVC 2\CodeTemplates*
There you can edit the generated code for each template, the template lines vary for each template so i cannot tell you, but you will spot them immidiately.
EDIT
The source for Web.Extensions is not available, but you can make use of relector and reflect on
\Microsoft Visual Studio \Common7\IDE\Microsoft.VisualStudio.Web.Extensions.dll assembly to see what "mvcHost" gives you :)
You will then notice a ViewDataType property which is a "Type", and you should then be able to create some logic around it to get the class name.
i keep getting Compilation errors when i try to do this . .is there anyway to do this inside the site.master file?
Did you add the master page as an MVC master page?
The Url property that you'rew trying to use comes from the MVC framework's ViewMasterPage class. If you added a regular master page, you need to make it inherit from ViewMasterPage. To do this, change the first line to
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
I am trying to implement a strongly typed master page and using this page as an example:
How to create a strongly typed master page using a base controller in ASP.NET MVC
I have the following declaration in my masterpage:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<InsTech.ForeSight.Web.Mvc.ModelBase>" %>
When I try to run I get:
Parser Error Message: Could not load type 'System.Web.Mvc.ViewMasterPage<InsTech.ForeSight.Web.Mvc.ModelBase>'
I am not really sure why it can't find the type. When I use the ViewMasterPage class everything is okay, but when I try to use the generic version it bombs.
Any Suggestions
Is InsTech.ForeSight.Web.Mvc.ModelBase in another assembly? if so, is it referenced?
Is InsTech.ForeSight.Web.Mvc.ModelBase abstract?
I got this error when my MVC Master page was outside the 'Views' folder (with my normal Web forms master pages), moving it there solved this error for me.
Edit: Sorry I didn't see #Whoiskb already posted the same answer in the comments up there. Just leaving this answer for people who don't read all the comments :)
i'm trying to make my site master page (views/shared/site.master) strongly typed.
eg. Inherits="TestProject.Mvc.Views.Shared.Site"
I can't seem to get this work. Once i make the site.master page strongly, typed, Visual Studio seems to 'loose' what <%= Html.XXX %> is. Also, the page throws an error when i try to display the default index route.
The SiteMasterViewData class exists in the views/shared/ folder and has been included at the top of the master page via..
<%# Import Namespace="TestProject.Mvc.Views.Shared"%>
Can this be done? is there a better way to do this?
Damn - found my own answer.
All masterpages in the ASP.NET MVC v1. need to inherit from:
<%# Master
Language="C#"
Inherits="System.Web.Mvc.ViewMasterPage" %>
so if u want to strongly type it, you can do this.
<%# Master
Language="C#"
Inherits="System.Web.Mvc.ViewMasterPage<SiteMasterViewData>" %>
HTH's other peeps :)
Not entirely sure what's going on here; any help would be appreciated.
I'm trying to create a new .NET MVC web app. I was pretty sure I had it set up correctly, but I'm getting the following error:
The type 'System.Web.Mvc.ViewPage' is ambiguous: it could come from assembly
'C:\MyProject\bin\System.Web.Mvc.DLL' or from assembly
'C:\MyProject\bin\MyProject.DLL'. Please specify the assembly explicitly in the type name.
The source error it reports is as follows:
Line 1: <%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
Line 2:
Line 3: <asp:Content ID="indexContent" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
Anything stand out that I'm doing completely wrong?
I suppose you named one of your page "ViewPage" is that the case?
And like #Jonathan mentioned, this smells:
Inherits="System.Web.Mvc.ViewPage"
On my MVC application, all the view pages have this instead:
Inherits="MySite.Views.Pages.Home"
Or something along the line. Your aspx page markup should have "Inherits" point to your code-behind class name, not the actual class that it is inheriting. The attribute name is rather misleading but its an artifact of earlier days.
Are you using a CodeBehind file, I don't see CodeBehind="" attribute where you are specifying the Inherits from? Then you have to point inherits to the class name of the codebehind.
Example:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcApplication4.Views.Home.Index" %>
Make sure the Inherits is fully qualified. It should be the namespace followed by the class name.
I'm running ASP.NET MVC Beta and also encountered this error.
It came about while I was trying to remove the code behind file for a view. I removed the "CodeBehind" attribute in the #Page directive and changed the "Inherits" attribute to point to System.Web.Mvc.ViewPage, but left the actual aspx.cs code behind file untouched in my project.
At this point, if I tried to run my project, I got the error you mentioned.
I resolved this error by deleting the aspx.cs (code behind) file for the view from my project.
Check your assembly references to System.Web.Mvc in web.config.
Mine were explicitly specifying 2.0.0.0, but my project referenced 3.0.0.0
This error usually indicates a class naming conflict. You are referencing two namespaces or you created a class with the same name in another namespace that you are using. I would start by looking at what that could be.
Inherits="System.Web.Mvc.ViewPage"
I'd imagine that this should be pointed at your View codebehind file class, not at the base ViewPage class.
Open up C:\MyProject\bin\MyProject.DLL in Reflector and look for ViewPage to see if you've defined one by accident.
No this should be a structural error message.
Check http://trikks.wordpress.com/2011/08/26/the-type-is-ambiguous-it-could-come-from-assembly-or-from-assembly-please-specify-the-assembly-explicitly-in-the-type-name/
I had the same exact error this week.
My Webform.aspx file had a generated designer.cs file. In this file the class was actually named "ViewPage". Delete the designer file, or move the class contents to the webform.aspx.cs file and my error was gone.
Just putting it out there in case someone had this error.