Confirmation mvc.net supports ValidationGroup concept

Based on the asp.net background, I really appreciated the concept of "validationGroup" when adding validation to the page. I was looking for the appropriate concept in mvc.net and didn't have much luck.

Is this concept available in mvc.net? If not, what are my alternatives?

+4
source share
3 answers

Unfortunately, no, he has nothing of the kind.

I wrote about a workaround when he returned.

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

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

+6
source

Charlino response extension, including HtmlAttributes and other ValidationSummary properties:

  public static MvcHtmlString ActionValidationSummary(this HtmlHelper html, string action, bool excludePropertyErrors, string message, object htmlAttributes = null) { var currentAction = html.ViewContext.RouteData.Values["action"].ToString(); if (currentAction.ToLower() == action.ToLower()) { return html.ValidationSummary(excludePropertyErrors, message, htmlAttributes); } return new MvcHtmlString(string.Empty); } 
+2
source

Charles's method was the only approach I could find that really worked for my purposes! (T.E. Two forms on the same MVC page β†’ without executing forms inside partial and ajax loads for partial numbers. This did not help me, since I wanted to return different result sets that would be displayed outside the div form, depending on what form was submitted)

I would advise making a small modification to the Html extension because you still want the validation summary to be displayed for an inconsistent check summary so that client-side validation works:

 namespace System.Web.Mvc { public static class HtmlExtensions { public static MvcHtmlString ActionValidationSummary(this HtmlHelper html, string action) { string currentAction = html.ViewContext.RouteData.Values["action"].ToString(); if (currentAction.ToLower() == action.ToLower()) return html.ValidationSummary(); return new MvcHtmlString("<div class=\"validation-summary-valid\" data-valmsg-summary=\"true\"><ul><li style=\"display:none\"></li></ul></div>"); } } } 
+1
source

All Articles