0

I am using Asp.net MVC4. I am having some entities and some rules for each entity which i need to validate from controller and display the curresponding error messages. I am trying to design a common validation class which can be used for all the entities that i am using. If I call the validation it should return me validation success or list of validation errors. I will be passing the entity and its type

Some sample entities and rules

    Employee - Employee should have either middle name or last name
             - First name, Middle name, last name should not be same
             - Should have address id and it should present in address table
             ......
             ......

     Address - In address line if there is an opening bracket it should have a matching closing bracket
             - If user give map url and it doesnt contains "http://" should show error message
            .......
            .......

I am having all the error message in a resource file with the error type id

Please advice me on which approch i should follow? or Share me some web tutorial link which will help me to design this

san
  • 1,859
  • 4
  • 26
  • 38
  • 1
    I will suggest you or have a view here: http://fluentvalidation.codeplex.com/ http://stackoverflow.com/a/16100455/3383479 –  Apr 09 '14 at 11:06

1 Answers1

1

Have you looked into Remote Validation? this could be a good case for what you are trying to achieve as you have some complicated rules.

Some example code:

public class ValidationController : Controller 
{

  public JsonResult IsAddressValid(string Address) 
  {

     //if Address is valid
     return Json(true, JsonRequestBehavior.AllowGet);

    //else

   return Json("Address Not valid", JsonRequestBehavior.AllowGet);

  }
}

Then on your model

public class SignupModel
{
    [Required]
    [Remote("IsAddressValid", "Validation")]
    public string Address{ get; set; }
}
testydonkey
  • 149
  • 4