MVC3 Unobtrusive Validation With MVC Contrib - 8/14/2011

Validation was a weakness of MVC until MVC3 came out with unobtrusive validation support. However, to get this feature you had to use the Microsoft provided MVC helpers. Having used MVC Contrib's helpers for a while, I found it frustrating to go back to the out of the box html helpers from Microsoft. After spending some time playing with it and doing research, I came up with a solution that I'm quite happy with that allows adding the unobtrusive validation attributes to the MVC Contrib controls.

The Markup


<% using (Html.BeginForm(a => a.Contrib())){ %>

        <%: Html.ValidationSummary(true) %>

        
TestModel
<%: Html.TextBox(a=>a.FirstName).Disabled(false) %> <%: Html.ValidationMessageFor(a=>a.FirstName) %>
<%: Html.DropDownList(a=>a.SomeValue).Options(Model.ListOfSomething,a=>a.Id,a=>a.Name) %> <%: Html.ValidationMessageFor(a => a.SomeValue)%>

<% } %>

Contrib Extensions


public static class MvcFavoritesExtensions

    {

        public static MvcContrib.FluentHtml.Elements.TextBox TextBox(this HtmlHelper htmlHelper, Expression> expression) where TModel : class

        {

            return new TextBox(expression.GetNameFor()).AddValidationAttributes(htmlHelper, expression);

        }

        public static MvcContrib.FluentHtml.Elements.Select DropDownList(this HtmlHelper htmlHelper, Expression> expression) where TModel : class

        {

            return new Select(expression.GetNameFor()).AddValidationAttributes(htmlHelper, expression);

        }

        public static MvcContrib.FluentHtml.Elements.CheckBox CheckBox(this HtmlHelper htmlHelper, Expression> expression) where TModel : class

        {

            return new CheckBox(expression.GetNameFor()).AddValidationAttributes(htmlHelper, expression);

        }

        private static T AddValidationAttributes(this FormElement element, HtmlHelper htmlHelper, Expression> expression) where T : FormElement

        {

            var validationAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression));

            

            foreach (var attr in validationAttributes)

            {

                element.Attr(attr.Key, attr.Value);

            }

            return (T)element;

        }

    }

A Sample ViewModel


public class TestModel

    {

        [Required(ErrorMessage = "First Name is Required")]

        public string FirstName { get; set; }



        public IEnumerable ListOfSomething { get; set; }



        [DisplayName("Some Value")]

        [Range(2,5,ErrorMessage = "Must be second or higher")]

        public int SomeValue { get; set; }

    }

The Rendered Markup


 
TestModel

Share this article


Other Blogs

Getting Started with the Telerik MVC Extensions - 2/12/2012
Codemash 2012 Recap & Pictures - 1/14/2012
Findlay Area .Net Users Group(FANUG) - Improving Software Quality with Continuous Integration, and An Introduction to FluentMigrator - 9/28/2011
MVC3 Unobtrusive Validation With MVC Contrib - 8/14/2011
Automating SSRS Report Deployment for CI - 7/10/2011