Problems with Site.Master and ASP.NET MVC - asp.net-mvc

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

Related

Problem with ASP.NET MVC Edtior Templates

I am trying to create an editor template for column in a Telerik MVC Grid. After clicking edit this simple string column should show the same string value and next to it I would like to show a button or image with an onclick event.
I CAN'T DO THIS!
I have found some really simple examples. Let's forget about the button or image for now and just display the same damn string, based on instructions found in this article:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
and:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= ViewData.TemplateInfo.FormattedModelValue %>
or:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Model %>
None of these work. In fact, when I step into this server side, ViewData.TemplateInfo.FormattedModelValue and Model don't have a value. What's going on? What is it in the grid that is preventing this? More complex Editor Templates like comboboxes persist the data back to the editor template.
How can I simply display the same text in an editor template with some further options like buttons that I will later change the data?
Steve
Steve, can you provide a sample of your controller code? It sounds as though it's not setting the model data correctly.

Telerik not showing in intellisense of VS 2008

I was coding an aspx page to use the telerik grid. But telerik is not showing in intellisense of VS 2008.
Following are the things done so far:
1. Added the telerik dll in the reference folder.
Code part
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage"%>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%# Import Namespace="Telerik.Web.Mvc" %>
Index
<% foreach(var v in (List<String>)ViewData["names"])
{
Response.Write(v);
%>
<br />
<%
}
%>
<% Html. %>
Thanks
The Visual Studio Intellisense in views is far from perfect. Personally what I do is to add the assembly (<assemblies> tag) and the namespace (<namespaces> tag) in web.config, so that it is available in all views. This way I no longer need to add any Import section in each view. After doing this you probably might need to reopen the view for the changes to take effect. Even restart Visual Studio. If it works at runtime and you don't get Intellisense at compile-time, well, I wish they improve it in future versions.

Modifying T4 code templates for ASP.NET MVC to get rid of full namespace reference

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.

can you use Url.Content() in a master page

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

Remove the *.cs, *.Designer.cs codebehind files?

Yeah, its a bit on this side of pointless, but I was wondering... I've got all these codebehind files cluttering my MVC app. The only reason why I need these files, as far as I can tell, is to tell ASP.NET that my page extends from ViewPage rather than Page.
I've tried a couple different Page directives changes, but nothing I've found will allow me to identify the base class for the page AND let me delete the codebehind files.
Is there a way to do it?
UPDATE: I'm trying to inherit from a strongly-typed ViewPage! Seems like its possible to inherit from a regular ViewPage...
Delete the codebehind and use a page directive like this:
<%# Page Title="Title" Inherits="System.Web.Mvc.ViewPage" Language="C#" MasterPageFile="~/Views/Layouts/Site.Master" %>
Or, if you want to get rid of the codebehind but still want to use strongly typed view, then read this link: http://devlicio.us/blogs/tim_barcz/archive/2008/08/13/strongly-typed-viewdata-without-a-codebehind.aspx
Here is a cut and paste of what this would look like:
<%# Page Inherits="System.Web.Mvc.ViewPage`1[[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" %>
Assuming you don't have any code in your codebehind, why don't you point them all to one codebehind file?
Straight out of the box you should be able to delete the .designer.cs and nothing will break. The other code behind can be useful, for instance if you'd like to strongly type your viewdata.

Resources