View Single Post
 
Old March 19th, 2009, 11:25 PM
dparsons dparsons is offline
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

As Yoda once said "You must unlearn what you have learned". ;]

The implementation of MVC that Microsoft provides will be considered by some to be a step backwards in that they way MVC pages are constructed are very similar to how a Classic ASP page would have been constructed. So in typical View (the UI) it will most certainly look something like this (this code can be found in the ViewArticle view in TBH):

aspnet Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/CenterStage.Master"
AutoEventWireup="true" CodeBehind="ViewArticle.aspx.cs" Inherits="TheBeerHouse.Views.Article.View" %>
 
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<div id="article-view">
<ul>
<li><strong>Rating: </strong>
<%= ViewData.Model.Votes%>
<%= ViewData.Model.Votes == 1 ? "user has" : "users have"%>
rated this article <span id="article-rating-value">
<% if (ViewData.Model.AverageRating > 0) { %><img src="<%= ImageRatingUrl %>" alt="<%= ViewData.Model.AverageRating %>" /><% } %></span></li>
<li><strong>Posted By: </strong>
<%= ViewData.Model.AddedBy%></li>
<li><strong>Views: </strong>this article has been read
<%= ViewData.Model.ViewCount%>
times</li>
<li><strong>Location: </strong>
<%= ViewData.Model.Location%></li>
</ul>
<%= ViewData.Model.Body%>
</div>
<form method="post" action="#" class="rate-article">
<p class="field input">
<h3>
<label for="rating">
What would you rate this article?</label></h3>
<br />
<select name="rating" id="rating">
<option value="0">0 Beers</option>
<option value="1">1 Beers</option>
<option value="2">2 Beers</option>
<option value="3">3 Beers</option>
<option value="4">4 Beers</option>
<option value="5">5 Beers</option>
</select>
<button type="submit" id="rate-button">
Rate</button>
</p>
</form>
<% if (ViewData.Model.CommentsEnabled) { %>
<div id="article-comments">
<h3>Comments</h3>
<% foreach (Comment comment in ViewData.Model.Comments) { %>
<% Html.RenderPartial("~/Views/Shared/Article/CommentItem.ascx", comment); %>
<% } %>
</div>
<form method="post" action="#" class="comment-create">
<input type="hidden" id="articleId" name="articleId" value="<%= ViewData.Model.ArticleID %>" />
<input type="hidden" id="commentId" name="commentId" value="" />
<p class="field input">
<label for="name">
Name</label><br />
<%= Html.TextBox("comment-name", null, new { @maxlength = 256 })%>
<span class="input-message"></span>
</p>
<p class="field input">
<label for="email">
E-Mail</label><br />
<%= Html.TextBox("comment-email", null, new { @maxlength = 256 })%>
<span class="input-message"></span>
</p>
<p class="field input">
<label for="body">
Body</label><br />
<%= Html.TextArea("comment-body", String.Empty)%>
<span class="input-message"></span>
</p>
<hr />
<p>
<button type="submit" id="comment-create-button">
Add Comment</button></p>
</form>
<% } %>
</asp:Content>

What most of you will notice right off the bat is that there is an abundance of server code tied up inside of the UI which was standard practice back in the days of ASP. Further more you will notice the use of multiple Forms which, again, was a practice employed by ASP. In most cases the codebehind for your views will be completely empty (in the case of the view above there is a public propery exposed in the code behind) and, in fact, won't even inherit from Page instead they will inheirt from ViewPage and, in the case of the article code above, it inherits an Enumeration of ViewPage with a type of Article (a view)!

Now before anyone throws their hands up in the air and says WTF you have to understand that this is the POINT of this design pattern. A typical Web Form, while some like to think it implements an MVC pattern, it truly doesnt. The ASPX page is tightly coupled to its code behind, period. This completely defeats the purpose of a seperation of layers and, by the shear nature of MVC, you are granted that seperation. A View is a View is a View. If will always house your UI and, typically, nothing more. It is the Models job to transform that data, and do any necessary processing that needs done before it is sent to the View for display. The Controller can be thought of, bascially, as your DAL.

People that have come to love the RAD evnironment of Visual Studio and love the "just works" nature of web forms will be sad to hear that with MVC:

-You have no ViewState. Data in form controls are good for a single page post + 1. If you want to remember data you have to write code that will physically repopulate a textbox or drop down list.
-You do have controls but not in the sense that you expect. For example a textbox is now represented as: <%= Html.TextBox(....) %>

So on and so forth.

Finally, by FUD, are you referring to Fear, Uncertantiy, and doubt? If so, why?

I will answer your questions to the best of my abilities.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
The Following User Says Thank You to dparsons For This Useful Post:
Avraham Nahir (March 20th, 2009)