ASP.NET MVC Preview 5 is out

And there are a few new and really neat features in the latest preview (ASP.NET MVC @ CodePlex). Next release will be the official Beta according to Phil: ASP.NET CodePlex Preview 5 Released

Let me just point to a few things from the release notes that caught my attention:

The IViewEngine interface now has a RenderPartial method that support the concept of rendering partial views. This is used in tandem with the Html.RenderPartial helper method. The important part is that your partial view can be rendered by a different view engine than the containing view. Say you have some formatting for your page and part of it is xml data that you need to transform. Then this would be very handy. Usage is <% Html.RenderPartial(...); %>. This method does not return a string, but instead renders to the underlying response’s supplied TextWriter instance.

There are two new useful attributes Added a new AcceptVerbs attribute. This attribute allows you to specify which HTTP verbs an action method will handle. If the current request’s HTTP method does not match the list of verbs that are specified in the attribute, from the perspective of the action invoker, the method does not exist. This allows you to have two action methods of the same name, as long as they respond to different HTTP verbs. What I did was the opposite; I routed two methods to the Index action and used one for GET and another for POST. It worked out just fine!

I added the simplest of forms

<% using( Html.Form<HomeController>(h => h.Index(), FormMethod.Post )){ %>
<%= Html.SubmitButton("Foo", "Bar") %>
<% } %>

If you're unused to MVC and the Html helper methods this might look strange. Especially the lambda. But here let me explain (in the words of Hanselman); The lambda does not get executed. It informs the routing what type of link to render. (In this case in a form tag). The logic is : If I called the Index action on the Home Controller (in this case via a post) what would the link look like. The main advantage of this approach is that the logic is type safe! If a name is refactored (HomeController or Index method) the code won't break.

Moving on. Here are the actions in the HomeController:

public class HomeController : Controller
{
    [AcceptVerbs("GET")]
    public ActionResult Index()
    {
        ViewData["Title"] = "Home Page";
        ViewData["Message"] = "GET on the Index action!";

        return View("Index");
    }

    [ActionName("Index"), AcceptVerbs("POST")]
    public ActionResult About()
    {
        ViewData["Title"] = "About Page";
        ViewData["Message"] = "POST on the Index Action!";

        return View();
    }
}

As you can see the GET is routed to the .Index() method and the POST for the same action (Index) is routed to the .About() method. Pretty cool actually! Now we can easily have better looking logic in our controller code so that we can, through attributes, clearly state what an action method handles. This will make a well evolved controller like CRUD on Domain Object "Foo" more easy to follow. A real CRUD controller can have at least 5 methods: List, Create, Insert, Edit, Update, Delete. The Create and Edit actions in this example map to the insert form view and the edit form view respectively. There are many ways to make a CRUD style controller, this is just a sample.

I like the new functaionality; It gives me a few new handy tools to play with. Looking forward to the Beta now so I can begin to try to put this into production. Usually customers don't want to begin developing on a technology that is not even a Beta. smile_wink

(Note: I had some really scary crash behavior after installing Preview 5 but it went away again so perhaps it wasn't anything dangerous. Visual Studio just dove down in a crash as I tried to open any of my Views! But as I said; it went away again.)

Cheers,

/Magnus

Technorati Tags: ,,

posted @ Sunday, August 31, 2008 8:58 AM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 1 and type the answer here:
 

Live Comment Preview:

 
Magnus Mårtensson
Senior Consultant .com
Contact