Tag Archives: mvc

Scott Guthrie Throws Some Water on the Web Forms/MVC Debate

26 Jan

Scott Guthrie wrote a very good article on about how to have a reasoned technical debate in response to the recent Web Forms / MVC flamewars that have been happening across the internet. Although I fall into the MVC camp it was good to see the Microsoft VP of the Developer Division try to calm people down. I believe Scott was trying to explain that both technologies are valid approaches to web development and that choosing one over the other for a project is not a reason to engage flamewars.

I have seen a lot of trashing of MVC by Web Forms adherents lately.  Web Forms advocates arguments break down into two categories reasoned opinions and FUD.  Here are some of the reasoned arguments I have seen.

  • they are already comfortable with the Web Forms page lifecycle
  • they don’t want to lose their investments in server controls that they have purchased or developed

Here are some of the arguments that just seem like FUD to me.

  • Web Forms are better for larger projects
  • Web Forms are more productive

Developers that have a lot of experience with Web Forms and a large investment in doing development using the Web Forms model and aren’t interested in MVC should stick with Web Forms. These are valid reasons for sticking with mature technology that isn’t going away anytime soon.

The other arguments however fall into the FUD category. Having just deployed a good size site built on ASP.NET MVC it works just fine for large projects. My team was definitely more productive using MVC than we would have been using Web Forms. Developers who are looking for more control over what is output to the page and a more agile development style should learn more about MVC.

When it comes to debating the merits of Web Forms and MVC the MVC camp is just as guilty when it comes to the religious wars that technology debates seem to turn into. Those who have made the switch to MVC really can’t understand why someone would stick with Web Forms and the limitations they impose on the developer. MVC developers see the page lifecycle and server controls as limitation while Web Forms developers see them as benefits. So what is really going on is developers trying to protect their ego’s by denigrating the choices others have made. This is basically a variation of the Emacs versus vi text editor wars.

It was nice to see someone of Scott Guthrie’s status in the community telling everyone to use their heads and try and use reasoned arguments and not go around blowing your stack at people who happen to disagree with you.

Update: Ian Cooper has written up a fantastic MVC or Web Forms comparison following up on Scott Guthrie’s article. Ian Cooper does a really great job summing up the argument for MVC and where Microsoft web development practices are going.

Styling Alternating Items in ASP.NET MVC Views

8 Sep

Here are a couple of ways to apply styles to alternating items in ASP.NET MVC views.

The jQuery way:

$(’ul > li:nth-child(even)’).addClass(’alt’);
$(’ul > li:first’).addClass(’first’);
$(’ul > li:last’).addClass(’last’);

Rick Strahl’s Odd/Even Looping Method:

<%
    bool oddEven = true;
    foreach (CodeSnippetListItem snip in snippetList)
    {
%>
    <div class="<%= (oddEven = !oddEven) ? "evenclass" : "oddclass" %>">
            Content goes here...
    </div>
<% } %>

ASP.NET MVC Model and Business Rules Validation

5 Sep

ASP.NET MVC Preview 5 is the release I have been waiting for from Microsoft.  The MVC team finally added easy model validation to the framework.  The new features are detailed at Scott Guthrie’s blog.  Validation was an issue that had previously hung me up when I dabbled with the MVC framework.  Model validation enables another business rules validation in a way that I thought was very elegant.  You can use partial classes to add business rules to a model class.  This allows your business rules to be separate from the model classes and if you regenerate your model classes you won’t overwrite your business rules.  I really like to see this because it means in my test project I can remove a layer of abstraction and keep the business rules close to the model classes.

I do have an issue with one of the new features in Preview 5 though and that is the [ActionName("action")] attribute.  In conjunction with the  [AcceptVerbs("POST")] attribute it allows you have different functionality for a URL depending on how its requested.  This is necessary functionality I just have issue with the way its implemented.  I would prefer to see something along the lines of this.

public object Create(string productName, Decimal unitPrice)
{
    if (Request.Verb == "GET")
    {
        return View("Create");
    }
    else if (Request.Verb == "POST")
    {
        // save the data RedirectToAction("List");
    }
}

I think this would make more sense.  You wouldn’t have the possibility of confusion as to what action to call your code.  You would always call the Create action.  If you started getting actions with a lot of lines of code in them you could just refactor the code in the if else statements into new methods.

What Hung Me Up On Using ASP.NET MVC Preview 4

13 Aug

I was delving into ASP.NET MVC Preview 4 for a small project and I got hung up on form validation.  Currently ASP.NET MVC provides no way to validate form input against the model objects.  Form validation has to be written client side in JavaScript and server side in the action.  I would really like to be able to decorate the LINQ-to-SQL objects fields with validation information like Rails does.  I also hope they add some validator helpers to the the HTML helpers.

I see that someone else ran into this and went ahead and coded a solution for it the Forms Framework with Validation for ASP.NET MVC.  This is an interesting approach to the problem I am not sure how it would deal with forms that are using more than one model.  I want Microsoft to address this feature rather than cobbling basic functionality together from a lot of little side projects that may not see any further progress after their initial development.

ASP.NET MVC Beginner Mistake

24 Jul

My last post was the result of making a beginners mistake and spending a lot of time being unable to resolve the issue.  Since I am using IIS 6 on XP/2003 I have to add .mvc to my controller paths to activate the ASP.NET MVC framework on a URL.  I was doing this and my controllers were working except for the / path of the web site. 

ASP.NET MVC still needs a default.aspx in the root of the web site.  The default.aspx just performs a redirect to the URL of whatever controller you are using for the site root. I had not changed the URL that default.aspx was redirecting to.  Once I changed the URL the redirect was using my problem was fixed.  I want to give a shout out to the official ASP.NET MVC forum for the help I got there.  Its nice to see that a Microsoft product is building an actual community around this technology as that more than anything will help it grow and evolve.

12