Create a folder called Infrastructure (the same level as Views, controllers, etc.).
Add New Class, LowHighCheck.cs
Use the code below to create your own validation attribute:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using System.Web; namespace YourNamespace.UI.Infrastructure { [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class LowHighCheck : ValidationAttribute { private string[] PropertyList { get; set; } public LowHighCheck(params string[] propertyList) { this.PropertyList = propertyList; } public override object TypeId { get { return this; } } public override bool IsValid(object value) {
Decorate your model with the attribute:
[LowHighCheck("LowValue", "HighValue", ErrorMessage = "your error message")] public class YourViewModel {
Remember to include the infrastructure namespace.
source share