The asp.net MVC community discusses the need for code-behind files when using Webformsviewengine, and whether this is an advantage or disadvantage or even a problem.
asp.net mvc still uses webformsviewengine by default. When you add a WebForms view to a asp.net MVC application using Visual Studio, a code-behind file and a designer file are automatically added. Many people think that code-behind files have been deprecated, and their usage is confusing because developers tend to use views like the traditional, asp.net Web forms.
Steve Smith further demonstrates the disadvantage of ASP.net MVC codebehind files because they lure developers to add logic to the view, which should be designed to remain "dumb":
Code-behind files are a temptation. Developers who are unfamiliar with asp.net mvc (asp.net mvc is less than a year old and not yet published) but have Web Forms development backgrounds (most) have a tendency to write code to hidden code files. This makes the logic in the view difficult to test, possibly even the logic contained in the code-behind file that invokes the database directly or the Web service so that it completely bypasses the separation of the model from the view.
Steve explains that in some cases the code-behind file is a bad but essential thing, such as if you want to reference the model in a strongly typed view by a strong type. Some people think that hidden code pages can also be used to store some of the view logic, while other ways may be confusing ASPX files. Last but not least, they have the support for smart tip technology in ASPX files. As for the first question, Tim Barcz demonstrates how to use a strongly typed ViewData with no code-behind, replacing the C # or VB notation by merging the CLR representations of generics:
inherits= "System.Web.Mvc.ViewPage ' 1 [[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]"
The CLR notation specifies the number of generic parameters, followed by the arguments and the assembly that contains the parameter type, after the single quotation marks.
Luis Abreu did not agree with Steve's usage and responded to the posting. "It's not certain that removing hidden code files can make up for an ' unqualified ' programmer," he says, because logic can still be placed in the script block of an ASPX file. Luis also disagreed with the general view of the "dumb" view. He thought the view might have mixed "show related code" and that the code should be placed in a hidden code file rather than in a controller or directly in an ASPX file, which would break the clear separation between the markup and the code.
View English Original: Http://www.infoq.com/news/2008/09/aspnet-mvc-codebehind