views:

2052

answers:

6

Trying to create a MVC User Control in the Release Candidate and I can't see to make one with a codebehind file. The same is true for MVC View pages.

Creating Views in the Beta would produce codebehinds...am I missing something?

A: 

Hi, I think this tutorial is what you are asking.. but not really sure what you want..

Ric Tokyo
Hi what I am saying is...in the Release Candidate version of MVC that was released today by Microsoft you now can not create a user control or a view page (a New File) with an attached codebehind. In the beta release of MVC this was possible.
RedWolves
+4  A: 

Code behind kind of defeats the purpose of the MVC Framework. Functionality should be kept separate from the view, the MVC team felt that code behind pages went against this ideology and therefore removed them.

Your can create a custom helper method to create your control. Also I'm not sure if MVC has view components (Monorail/Castle) but that could be an option as well.

+1  A: 

The whole idea for ASP.Net-mvc was to get rid of the codebehind files...thats why asp web controls didnt matter that most didn't work.But with the changes of getting rid of the code behind comes with a different programming style..The idea is codebehind files are EVIL:

http://stevesmithblog.com/blog/codebehind-files-in-asp-net-mvc-are-evil/

the whole idea is to make sure people remember they are using asp.Net-mvc and not asp.et web pages. take alook at this link ,it explains it a little better:

http://blog.lozanotek.com/archive/2008/10/20/Visual_Studio_Templates_for_MVC_Views_without_Codebehind_Files.aspx

TStamper
+3  A: 

From ScottGu's Blog post:

*Views without Code-Behind Files Based on feedback we’ve changed view-templates to not have a code-behind file by default. This change helps reinforce the purpose of views in a MVC application (which are intended to be purely about rendering and to not contain any non-rendering related code), and for most people eliminates unused files in the project. The RC build now adds C# and VB syntax support for inheriting view templates from base classes that use generics. For example, below we are using this with the Edit.aspx view template – whose “inherits” attribute derives from the ViewPage type:

One nice benefit of not using a code-behind file is that you'll now get immediate intellisense within view template files when you add them to the project. With previous builds you had to do a build/compile immediately after creating a view in order to get code intellisense within it. The RC makes the workflow of adding and immediately editing a view compile-free and much more seamless.

Important: If you are upgrading a ASP.NET MVC project that was created with an earlier build make sure to follow the steps in the release notes – the web.config file under the \Views directory needs to be updated with some settings in order for the above generics based syntax to work.*

RedWolves
A: 

This is great except that now, you cannot debug in the aspx file. I have tried several times to debug in my aspx file with no anvil. I'm I doing something wrong or this is by design?

Can you be more specific about your issue?
greg
+3  A: 

I answered this question here:

How to add a Code-behind page to a Partial View

Seems this wasn't particularly tricky, and is quite do-able This answer worked for a Partial 'ViewUserControl' but the same should apply

Ok.

First: Add a Class file with the convention of .cs (i.e. view.ascx.cs)

Second: Add "using System.Web.Mvc;" to the class

Third: Change the Class to Inherit from "ViewUserControl<>"

Fourth: Add the following to the View's header:

CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"

Fifthly: Copy the files out of the solution and drag back in to reassociate the two together

Note: For this to work with a Normal MVC View you just need to inherit the class from "ViewPage"

Harry