Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does mvc.net validation support the concept of ValidationGroup

Coming from the asp.net background, I really appreciated the concept of 'validationGroup' when adding validation to a page. I've been searching for a corresponding concept within mvc.net and haven't had much luck.

Is this concept available in mvc.net? If not, what alternatives do I have?

like image 280
yamspog Avatar asked Jan 21 '23 09:01

yamspog


1 Answers

Unfortunately no, it doesn't come with anything like that.

I blogged about a workaround a wee while back.

ASP.NET MVC - Validation Summary with 2 Forms & 1 View

The jist of the blog post:

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;
        }
    }
}

And

<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>

HTHs,
Charles

like image 100
Charlino Avatar answered May 20 '23 13:05

Charlino