Thursday, December 22, 2011

Multiple validation summary at single page

The problem:
at razor , if we have 3 partial views each contains a validation summary control
when the validation works, and the validation summary supposed to give the usual red text , all the validations summary controls will view the error , even if the element that have the problem is not it its own view
example 

The solution:
the solution is to draw the validation summary only when you validate your form for example
  1. at the controller of "RegisterForm" , set the  Session["ValidationSummary"] = "RegisterForm"
  2. at the controller of "LoginForm" , set the  Session["ValidationSummary"] =  "LoginForm"
  3. at the   RegisterForm view
                    if ( Session["ValidationSummary"] == "RegisterForm")
            { 
   @Html.ValidationSummary(); 
            } 

    4. at the    LoginForm  view 
       if ( Session["ValidationSummary"] == " LoginForm ")
            { 
                   @Html.ValidationSummary(); 
            } 

       



you can find nother solution here NamedValidationSummary 



No comments: