ASP.NET MVC 2 Preview 1 released!

Edit on GitHub

Today, Phil Haack did a blog post on the release of ASP.NET MVC 2 Preview 1! Get it while it’s fresh :-) An updated roadmap is also available on CodePlex.

Guess now is about time to start revising my ASP.NET MVC 1.0 Quickly book…

kick it on DotNetKicks.com

New features in ASP.NET MVC Preview 1

Templated helpers

Templated helpers are not new: ASP.NET Dynamic Data already used this feature. Basically, you are creating a default control when you want to display/edit a specific data type in a view. For example, a System.String will have a user control defined that renders a textbox. However, if you want this to be a TinyMCE control by default, you’ll have to change the templated helper in one place and you’re done.

More concrete: create a new view in your application: Views\Shared\DisplayTemplates\String.ascx. The code for that view would be:

[code:c#]

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<strong><%= Html.Encode(Model) %></strong>

[/code]

There you go: every string you want to put in a view using the <%= Html.DisplayFor(c => person.Name) %> HtmlHelper will render that string in bold.

Note that your domain class can also use UI hints to specify the templated helper to use when rendering:

[code:c#]

public class Person {
    [UIHint("NameTextBox")]
    public string Name { get; set; }

    // ...
}

[/code]

This will make sure that when Person’s Name is rendered, the NameTextBox.ascx control will be used instead of the default one.

Areas

Finally, native support for areas! Areas help you split your application into more logical subsections, which is useful when working with large projects.Each area is implemented as a separate ASP.NET MVC. When compiling, ASP.NET MVC invokes a build task which merges all areas into the main application.

Check MSDN for a detailed example on using areas. I’ll get this one in MvcSiteMap as soon as possible.

Support for DataAnnotations

The new ASP.NET MVC 2 default model binder makes use of the System.ComponentModel.DataAnnotations namespace to perform validation at the moment of binding data to the model. This concept was used for ASP.NET Dynamic Data, recently picked up by the RIA services team and now also available for ASP.NET MVC.

Basically, what you have to do in order to validate your domain objects, is decorating the class’properties with some DataAnnotations:

[code:c#]

public class Person {
    [Required(ErrorMessage = "Name is required.")]
    [StringLength(60, ErrorMessage = "Name should not exceed 60 characters.")]
    public string Name { get; set; }

    // ...
}

[/code]

Easy no? Now just use the model binder inside your controller and validation will occur “automagically”.

Also check my blog post on TwitterMatic for another example.

HttpPost attribute

A small update: [AcceptVerbs(HttpVerbs.Post)] can now be written as [HttpPost]. Easier to read IMHO.

This means that:

[code:c#]

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Person person) {
    // ...
}

[/code]

becomes the following:

[code:c#]

[HttpPost]
public ActionResult Create(Person person) {
    //...
}

[/code]

DefaultValueAttribute

Default parameter values in an action method can now be specified using an attribute. This attribute currently only seems to support primitive types (such as integers, booleans, strings, …). Here’s an example:

[code:c#]

public class PersonController : Controller {
    public ActionResult Create([DefaultValue("Maarten")]string name) {
        // ...
    }
}

[/code]

kick it on DotNetKicks.com

This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.

Leave a Comment

avatar

0 responses