views:

625

answers:

4

I'm working with Visual Studio 2008 SP1 and ASP.NET MVC v1. When right clicking on a view I do not get the option "Convert to Web Application" that I would need to generate code behind .cs classes. I see that option for the actual project and folders, but not for views (aspx files). I've checked the ProjectTypeGuids to have the "right" (?) values:

{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}

Any other suggestions as to what I could look for?

Thanks.

(I am aware of design implications of using code behind classes with MVC)

P.S. To do it manually all you have to do is:

  1. Add a file with the same name as your view and the .cs (or .vb) extension, for example Index.aspx.cs. Make sure you modify your class to inherit from System.Web.Mvc.ViewPage or some other class that inherits from that.
  2. Edit the aspx file and add to the @Page directive CodeBehind="Index.aspx.cs" and change Inherits to "MyNamespace.Views.Home.Index" (obviously you need to have the right code behind and namespace there).
  3. Right click on the aspx file and choose Convert to Web Application. This will create the design file and also modify your .cs class and mark it as "partial".
+3  A: 

The MVC development model does not need code behind.

Read a good Blog Post on this Here

Jeremy Petzold
Like I mentioned in the last sentence I'm aware of these design implications (i.e. debate). This post is not about rehashing those arguments. Thanks.
pbz
+4  A: 

There's no need to use 'code-behind' with ASP.NET MVC.

If you use a 'code-behind', you're not following the convention of ASP.NET MVC.

The question is, why do you want a code-behind? Answering that will help us to determine what you really need.

If you really want to do this, you can do it by mixing Webforms and ASP.NET MVC together. There are lots of resources on this, but here's just one.

George Stocker
I'm looking into avoiding peppering my views with view-related code. For example, some controls are designed to use events that require functions for processing. I do not want all that stuff to be mixed with the actual HTML. Thanks.
pbz
You shouldn't be using ASP.NET server controls with ASP.NET MVC since they couple the controller and view. Why use ASP.NET MVC if you aren't going to conform to its strengths?
George Stocker
Because there are a number of controls that I would like to reuse, rather than rebuild them in the MVC way. These particular controls do not couple with the controller in any way.
pbz
+4  A: 

"Convert to web application" is a project/file-level command. You can't use it on a single ASPX file.

Also, there is no alternative automated way (that I know of :-)) to add code-behind files to an ASPX file. You have to do it manually, by adding the relevant files yourself and then adding them to the .csproj.

Franci Penov
I've used this before with MVC, even ScottGu mentioned that in one of his replies here: http://weblogs.asp.net/scottgu/archive/2007/12/06/asp-net-mvc-framework-part-3-passing-viewdata-from-controllers-to-views.aspx#5441681'Just select your file in the solution explorer, right click, and choose the "Convert to Web Application" context menu option. This will generate the .designer file for you.'Either the MVC templates changed to where this is no longer possible or my installation is corrupt (I actually did reinstall VS just to be sure). Thanks.
pbz
P.S. Now that I think about it, that may have been with one of the RCs and not with the final version (1.0), though I can't remember exactly. Could it be that they removed the creation of .cs files in the MVC templates and since there's no .cs file there's no "Convert to Web Application" option (which would only generate the .designer.cs file)? For some reason I remember being able to right click on an aspx file, choose that option and VS would generate the two files for me; maybe I remember it wrong...
pbz
Yes, that was in Preview 5 of Version 1. It's changed drastically since then. :-)
George Stocker
@George: So the "official story" is that you can't do that (unless you do it by hand)? Thanks.
pbz
A: 

If you're trying to reuse some controls, maybe a good approach is to create and render them inside a helper method and than call that method from the view.

What I'm thinking about would be something like this:

public static string HelperMethod(param_list)
{
   var control = new ControlType();
   //set up control properties according to param_list

   //get the html as string - one way to do it would be like this
   StringWriter stringWriter = new StringWriter();
   HtmlTextWriter htmlWriter= new HtmlTextWriter(stringWriter);
   control.RenderControl(htmlWriter);
   string result= stringWriter.ToString();
}

And then call it from the view like this:

<%= HelperClass.HelperMethod(params) %>

I'm not sure if this approach will work, I don't know even if it makes sense. It's more of I hack than a proper solution. I haven't done anything like this before, it's just an idea, try to see if it helps you. You should also have in mind that ASP.NET controls usually use the ViewState for state management and that there is no such thing in ASP.NET MVC.

Victor