Just a quick post to hopefully save others some time. ASP.NET MVC 2.0 has built-in support for client-side validation, but it doesn’t work with the expression-based BeginForm HtmlHelper methods that are available in the ASP.NET MVC Futures assembly. I also don’t like that I have to call a separate helper method to set my form up for validation. To address these two limitations, I wrote my own extension method:
public static class ValidationHelper { private const string LAST_FORM_ID_KEY = "_lastFormId"; public static MvcForm BeginValidatedForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action) where TController : Controller { helper.EnableClientValidation(); var id = GetNextFormId(helper); TagBuilder builder = new TagBuilder("form"); string str = helper.BuildUrlFromExpression(action); builder.MergeAttribute("action", str); builder.MergeAttribute("method", "POST"); builder.GenerateId(id); helper.ViewContext.HttpContext.Response.Write(builder.ToString(TagRenderMode.StartTag)); var form = new MvcForm(helper.ViewContext); helper.ViewContext.FormContext.FormId = id; return form; } private static string GetNextFormId(HtmlHelper helper) { int count = (int) (helper.ViewContext.HttpContext.Items[LAST_FORM_ID_KEY] ?? 0); count++; helper.ViewContext.HttpContext.Items[LAST_FORM_ID_KEY] = count; return string.Format("form{0}", count); } }
Now you can simply do like this:
<h1>Sign-up</h1> <p>Signing up is easy <em>and</em> free!</p> <% using (Html.BeginValidatedForm<SignupController>(c => c.Create(null))) { %> <fieldset> <div> <p>1) Choose a username:</p> <%=Html.EditorFor(m => m.Username) %><%=Html.ValidationMessageFor(c => c.Username) %> </div> <div> <p>2) What's your E-mail address? <span class="note">(<a href="#">Why do we need this?</a>)</span></p> <%=Html.EditorFor(m => m.Email)%><%=Html.ValidationMessageFor(c => c.Email) %> </div> <%=Html.SubmitButton("submit", "Start Raging Now!") %> </fieldset> <%} %>
and get client-side validation (assuming you’ve set everything else up correctly).
What a great read. My mind is buzzing now, thanks 🙂
Hi webmaster, your site’s design is fantastic and loving it. Your posts are superb. Please continue this great work. Greets!
The actor – Brad Pitt – what can you say? Real celebrity; magnificent professional; and fantastic, seriously believable communication & presentation skills. And congatulations on the recent film. Getting excited about your next picture.
A place I found had lots of great info on <a href="http://www.hotgamedownloads.info">Free downloads</a> and was really insightful. Its really difficult to find good info
Additional info is here: http://www.flicspasalon.com/free-handmade-soap.html
Hello thanks for taking the time to maintain a nice blog like this. I really admire it. I feel that we should do our bit and mention this post on facebook.
Nice to finally see somebody who can do a proper blogpost without spelling errors or lousy grammar.
I appreciate your blog very much. Will read all. Keep up to briliant writing on it. ty
Your blog has a really cool design. That being said the information here is free and is of high-quality. I am subscribing to your feed right now.
We would be interested in advertising on this site. Can you please contact us? Thanks
How to subscribe by email?
The process of oil painting varies from artist to artist, but often includes certain steps. First, the artist prepares the surface. Although surfaces like linoleum, wooden panel, paper, slate, pressed wood, and cardboard have been used, the most popular surface since the 16th century has been canvas, although many artists used panel through the 17th century and beyond. Panel is more expensive, heavier, harder to transport, and prone to warp or split in poor conditions. For fine detail, however, the absolute solidity of a wooden panel gives an advantage.
Cheers for this particular informative stuff. Often, the very best answers originate from the sources a person would not expect. Not long ago, I decided not to give much thought to leaving comments on weblog posts and have left responses even less. Reading your good piece of writing, is going to encourage me to do this more regularly.
Thats a great article, thanks for writing it. I’ve bookmarked your site and will be eager to reading more!
I was hoping to instantly register for your rss feed to keep updated of any changes on your blog, but I couldnt find it, where is the link for it?