ASP.NET MVC 5.1 – Whats New

On Jan 20 2014, Microsoft released NuGet package for yet another version of ASP.NET MVC and it is at version 5.1 now. Lets take a lap around what are the new things in ASP.NET MVC 5.1 in this blog post

Improvements in Attribute Routing:

Attribute Routing was first introduced in ASP.NET MVC 5 which was released with Visual Studio 2013 or .NET 4.5.1. With ASP.NET MVC 5.1 the improvements is done in the following areas:

  • Constraints in Attribute Routing
  • Route Versioning
  • Route selected based on Header

Microsoft has published a sample on Attribute Routing here: https://aspnet.codeplex.com/SourceControl/latest#Samples/MVC/AttributeRoutingSample/

I will be delving into these improvements in my future posts. For now, you can get the sample code and play with it.

Enum support in Views

With ASP.NET MVC 5.1 views get new helper to use Enum as list. Lets say you have a Enum and you want show the enum values as a dropdownlist, now you have a Html helper for that which is called “EnumDropDownListFor”. Also provided is a EnumHelper.GetSelectList() which will provide the Enum values as a list for you to manually loop and use it in your constructs. Here is a sample that ASP.NET team has put up for this helper: https://aspnet.codeplex.com/SourceControl/latest#Samples/MVC/EnumSample/EnumSample/

Editor Template’s Support Attributes:

Prior to 5.1, the EditorFor() helper that is available for building form elements in MVC views was not allowing any html attributes that could be passed. With 5.1, the EditorFor() helper has been improvised and allows you to pass html attributes as anonymous objects. Here is an example of how you use the new syntax:

@Html.EditorFor(model => model, 
                new { htmlAttributes = new { @class = "form-control" } })

Unobtrusive Validation supports Min/Max Length:

With 5.1 release, unobtrusive client side validation of string and array types will now be supported for properties attributed with MinLengthAttribute & MaxLengthAttribute. So you can just decorate your property lets say Name with a Min and Max length attribute on your Model and on the client side the unobtrusive validation will respect that and provide you the necessary checks. I will be blogging about this in a future post.

 Support for current Context in Unobtrusive Ajax:

When you are using Unobtrusive Ajax in your MVC Views, the callbacks did not have a mechanism to locate the invoking element in MVC 5. With 5.1 release, Microsoft has provide the context of the invoking element through the “this” keyword. So all callback functions like OnBegin, OnComplete, OnFailure and OnSuccess now support the “this” object which will give you the invoking element. Here is a quick example of this usage:

@Ajax.ActionLink("Click me", "AjaxAction", 
                 new AjaxOptions { UpdateTargetId = "foo", 
                     OnBegin = "OnClick" })
<script>
function OnClick(jqXHR) 
{
   console.log($(this));
}
</script>

How to get ASP.NET MVC 5.1

ASP.NET MVC 5.1 is completely a NuGet package. Before you can upgrade yourself to ASP.NET MVC 5.1, you will need to make sure that you have net the following pre requisite:

Once you have the above pre requisites met, you can just issue a NuGet command to download the 5.1 bits to your project. First create a ASP.NET MVC 5 project. Then go to “NuGet Package Manager Console” and issue the command: Install-Package Microsoft.AspNet.Mvc -Version 5.1.0

Alternatively you can right click on your project and select “Manage NuGet Packages” and search for “Microsoft.AspNet.Mvc” package and install it.

SNAGHTMLa878e9f

This was a quick summary of what’s new in ASP.NET MVC 5.1. Hope you got a quick lap around what’s new. I will be delving into each aspect in future posts.

Till next time – Happy Coding. Code with Passion, Decode with Patience.

Tagged on: , ,
  • Rabel Obispo

    Outstanding!
    Thank you, for the update.