Showing posts with label Razor. Show all posts
Showing posts with label Razor. Show all posts

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 



Error with MVC telerik


when using Telerik components , it may give you the erorr:
 "Unable to set value of the property '__MVC_FormValidation': object is null or undefined"
 solution :
 let the style sheets registeration to be:
 @(Html.Telerik().StyleSheetRegistrar() .DefaultGroup(group => group
                                                                                   .Add("telerik.common.css")
                                                                                    .Add("telerik.Vista.css")
                                                                                   .Add("telerik.rtl.css") ) )

 instead of :
 @(Html.Telerik().StyleSheetRegistrar() .DefaultGroup(group => group
                                                                                   .Add("telerik.common.css")
                                                                                    .Add("telerik.Vista.css")
                                                                                   .Add("telerik.rtl.css")
                                                                                  .Combined(true)
                                                                                   .Compress(true) ) )

Wednesday, November 16, 2011

MVC validation by Data Annotation ---Razor view --- Entity Framework 4.1

1- create parial class with the same namespace of the original class that is generated with the Entity framework :
namespace myNamespace
{
[MetadataTypeAttribute(typeof(Metadata))]
public partial class MyClass
{
private class Metadata
{
[Required]
public object Property_1{ get; set; }
}
}
}
2-enable client validation in the view:
@{ Html.EnableClientValidation(); }
@using (Html.BeginForm())
{

}
3- add the validation controls :
@Html.ValidationMessage("Property_1","*") // where "Property_1" is the property name which need to be validated

4- modify the controller to handle the validation : for example in the action we provide 2 different pathes , one for isValid== true another for is valid == false:
for example:
if (ModelState.IsValid)
{
// do work
}
else
{
// another work
}