<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx</link><description>Steve Smith has an interesting post with a suggestive name: “Codebehind files in ASP.NET MVC are evil”. So, it seemed appropriate to write a response with the title “Codebehind files in ASP.NET MVC * are not * evil”. I just couldn’t resist it :) Technically</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1684411</link><pubDate>Fri, 03 Apr 2009 08:11:03 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684411</guid><dc:creator>luisabreu</dc:creator><description>&lt;p&gt;Yes, it is...but It&amp;#39;s the &amp;nbsp;one that has been most used to justify the change. and when you think about it, it&amp;#39;s really as weak as yours...if your programmers are really &amp;quot;smart&amp;quot; and follow your standards, then having or not having &amp;nbsp;a code-behind file isn&amp;#39;t really that important, right? &lt;/p&gt;
&lt;p&gt;Regarding the spaghetti approach, well, if you use the available bits and decide to go with the web form view engine, there&amp;#39;s not much you can do about it: you&amp;#39;ll end up having lots of &amp;lt;% %&amp;gt; through your code. &lt;/p&gt;
&lt;p&gt;And I don&amp;#39;t agree with you...the entire point of MVC is not to use controllers. In my opinion, its advantage is the division of responsibilities (I might be stating the obvious, but the view is code too!)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684411" width="1" height="1"&gt;</description></item><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1684396</link><pubDate>Fri, 03 Apr 2009 05:45:16 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684396</guid><dc:creator>Dave Schinkel</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;&amp;gt;On the other hand, I’m not sure that removing the codebehind file will be enough for stopping a “bad” programmer&lt;/p&gt;
&lt;p&gt;That&amp;#39;s a weak argument. &amp;nbsp;That&amp;#39;s why you have team standards and code reviews. &amp;nbsp;And you don&amp;#39;t hire dumb programmers that don&amp;#39;t care about these standards when they code.&lt;/p&gt;
&lt;p&gt;If you communicate often these standards to your team, then the worry about spaghetti in your View should not be so and therefore get rid of code-behind. &amp;nbsp;The entire point of MVC is to use controllers.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684396" width="1" height="1"&gt;</description></item><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1675713</link><pubDate>Wed, 04 Mar 2009 22:18:38 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1675713</guid><dc:creator>Rusty</dc:creator><description>&lt;p&gt;I agree! &amp;nbsp;Code behind is a perfect way to process presentation _logic_. &amp;nbsp;I believe very strongly that you should perform all your business logic and data processing in and from your controller. &amp;nbsp;You should format things for display and get everything in order before rendering your view. &amp;nbsp;However, things like css class is pure view and should NOT be passed from the controller. &amp;nbsp;I like your example where you check a condition and select the appropriate css class based on a model boolean. &amp;nbsp;Why not select css class in your view? &amp;nbsp;Because the inherent ability to select different views makes the mechanism for changing presentation a property of the view, not the controller. &amp;nbsp; The controller sets the condition, the view presents that appropriately. &amp;nbsp;As a perfect example, imagine an action that renders html when called normally and JSON when called via ajax. &amp;nbsp;In that case, css class is not the correct way to pass the information to the view and is, in fact, a mistake when an ajax call selects the JSON view.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1675713" width="1" height="1"&gt;</description></item><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1663880</link><pubDate>Wed, 21 Jan 2009 00:47:23 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1663880</guid><dc:creator>Maxwell</dc:creator><description>&lt;p&gt;I have to say I agree 100% with your comments.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1663880" width="1" height="1"&gt;</description></item><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1649436</link><pubDate>Wed, 01 Oct 2008 19:50:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649436</guid><dc:creator>@Luis</dc:creator><description>&lt;p&gt;There are times when a code-behind file is absolutely necessary. &amp;nbsp;For those times, I&amp;#39;d like to see &amp;nbsp;a right-click to add the code-behind. &amp;nbsp;For the rest of the vast majority of views, I&amp;#39;d like a template that doesn&amp;#39;t have a code-behind&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649436" width="1" height="1"&gt;</description></item><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1648453</link><pubDate>Sun, 21 Sep 2008 12:44:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1648453</guid><dc:creator>celik</dc:creator><description>&lt;p&gt;Well said. Thank you. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1648453" width="1" height="1"&gt;</description></item><item><title>ASP.NET MVC Archived Blog Posts, Page 1</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1648435</link><pubDate>Sun, 21 Sep 2008 02:25:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1648435</guid><dc:creator>ASP.NET MVC Archived Blog Posts, Page 1</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;ASP.NET MVC Archived Blog Posts, Page 1&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1648435" width="1" height="1"&gt;</description></item><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1648344</link><pubDate>Fri, 19 Sep 2008 19:08:53 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1648344</guid><dc:creator>Mike</dc:creator><description>&lt;p&gt;Well said Craig. I&amp;#39;m tired of bloggers arguing that because something can be abused it should be taken away. Too easy to score page views with that stuff.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1648344" width="1" height="1"&gt;</description></item><item><title>re: Codebehind files in ASP.NET MVC *ARE NOT* evil</title><link>http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx#1648325</link><pubDate>Fri, 19 Sep 2008 15:12:56 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1648325</guid><dc:creator>Craig Shoemaker</dc:creator><description>&lt;p&gt;Luis:&lt;/p&gt;
&lt;p&gt;I know that this stance is not a popular one, so thanks for posting this... &lt;/p&gt;
&lt;p&gt;I have to say I agree with you on both of your major points.&lt;/p&gt;
&lt;p&gt;I am a big proponent of keeping code in its appropriate place. There are times when you simply have presentational logic. Sure I think you should think twice any time you write an &amp;quot;if&amp;quot; statement in your codebehind, but that doesn&amp;#39;t mean it&amp;#39;s NEVER appropriate. &lt;/p&gt;
&lt;p&gt;Why should we hinder the capabilities of the view just b/c we are afraid someone will abuse it? Perhaps we should add a validator to our designer to check to see if people are using tables for layout too! :)&lt;/p&gt;
&lt;p&gt;Also I still haven&amp;#39;t heard a compelling reason why we can&amp;#39;t just use a streamlined control model as you suggest. All I have heard is &amp;quot;that&amp;#39;s not how we do it&amp;quot;. Perhaps someone has given a good reason and I just missed it - but light-weight server controls seem to make a lot of sense.&lt;/p&gt;
&lt;p&gt;Thanks for the post!&lt;/p&gt;
&lt;p&gt;Craig&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1648325" width="1" height="1"&gt;</description></item></channel></rss>