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
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; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.