About your host

Photo of your host - Charles Vallance Charles Vallance is a web developer with a slight case of OCD when it comes to nice clean standards compliant html and code.

Badges

twitter / cvallance
My articles have been featured in The Morning Brew - Daily .NET News and Views

Tag Cloud

more tags...
posted @ Wednesday, July 15, 2009 7:59 PM

I currently have a situation where I have two separate actions that rendered the same view and then on this view it has two individual forms which post to the two aforementioned controller actions.

Each of the forms have their own separate ValidationSummary() calls to display their validation errors. But when I posted to either one of these actions and the validation failed, I quickly realised a short coming of the built in ValidationSummary() helper method… you see, it doesn’t have a clue about which form has the errors!

Confused yet? Oddly enough, so am I.

A screen shot should clear things up:

register-login

And the fail:

register-login-fail

Making sense now? Good...

Anyway, I came up with an easy enough work around so I thought I would share it.

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static string ActionValidationSummary(this HtmlHelper html, string action)
        {
            string currentAction = html.ViewContext.RouteData.Values["action"].ToString();
            
            if (currentAction.ToLower() == action.ToLower())
                return html.ValidationSummary();
            
            return string.Empty;
        }
    }
}

I'm (basically) calling it like so:

<h2>Register</h2>

<%= Html.ActionValidationSummary("Register") %>

<form method="post" id="register-form" action="<%= Html.AttributeEncode(Url.Action("Register")) %>">

... blah ...

</form>


<h2>User Login</h2>

<%= Html.ActionValidationSummary("LogIn") %>

<form method="post" id="login-form" action="<%= Html.AttributeEncode(Url.Action("LogIn")) %>">

... blah ...

</form>

And that's it. Enjoy.

Technorati Tags:

And then he said...

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by Andrew Gunn
on 7/16/2009 1:47 AM
Nice spot! Suprised I (and many others) haven't ran into the same problem sooner. Good fix too!

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by jsmith
on 8/12/2009 5:32 AM
Isn't that what the 'validationGroup' property is for?

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by charles
on 8/12/2009 12:32 PM
@jsmith This posting was specific to ASP.NET MVC... 'validationGroup' is a property that only appears in ASP.NET WebForm controls.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by jsmith
on 8/13/2009 12:31 AM
Ah, missed that somehow, gotcha!

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by Softlion
on 1/2/2010 11:35 PM
Thanks !

Have you found an easy way to fix all the Html helpers which automatically set the field-validation-error and input-validation-error classes when their validation failed, even if they do not belong to this form ?

Ok just set different ids for each ...
But that way we lost the auto fill from the browser.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by Car Market
on 1/11/2010 9:49 PM
very well done ... although that should have been built-into the Validation summary algorithm ... thanks for sharing ... nice work

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by Charles
on 1/17/2010 12:43 AM
Softlion - the only solution there is to have different ids. It frowned upon to have ids the same in your page for the simple reason that your html won't be valid if you have elements with the same id.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by Softlion
on 2/24/2010 12:48 AM
Charles: Sure.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by Online Kasino Boni
on 6/27/2010 12:45 AM
Thank you for creating this article.It really helped to implement RSS feed to my website.One question I have is on how to provide a hyperlink or a selectable entity from the data list for of all titles displayed.Upon clicking the title,a new window should be opened with the details content associated with that title. ...

# Absolutely fantastic posting!

Gravatar
Posted by premium wp themes
on 7/22/2010 7:33 PM
Absolutely fantastic posting! Lots of useful information and inspiration, both of which we all need! Thanks for the sharing information we will wait also for your new article keep posting.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by alltip
on 7/27/2010 8:44 PM
Thank you for this Article.
I am facing same Problem.and i find finally Solution.

alltip

# premium wordpress themes Coupon Code

Gravatar
Posted by JACK
on 8/18/2010 12:41 AM
You had some nice ideas in the article, I enjoyed reading it.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
on 8/18/2010 12:42 AM
Very Nice site, I just came to know of it yesterday morning. Bookmarked this and also stumbled upon it. Thanks a lot.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
on 8/24/2010 5:25 PM
I wanted to thank for this great read!I really enjoyed reading.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
on 8/27/2010 7:25 PM
Thanks for sharing ValidationSummary() method...really with one we can get a lots of programming...very useful...your representation in the form of picture is really nice one...I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing.

# Mr

Gravatar
on 8/29/2010 8:59 AM
Excellent and very exciting site. Love to watch. Keep Rocking.

# re: ASP.NET MVC – Validation Summary with 2 Forms & 1 View

Gravatar
Posted by John875
on 9/3/2010 6:26 AM
The health care reform surprised me that it actually passed, can't see how it passed with no republican votes! office furniture Brisbane

And then I said...

Title *
Name *
Email
Url
Comment *  
Please add 5 and 2 and type the answer here: