Results 1 to 3 of 3

Thread: [RESOLVED] [MVC3] Implementing a custom validation attribute for CompareTo

  1. #1

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Resolved [RESOLVED] [MVC3] Implementing a custom validation attribute for CompareTo

    The System.Web.Mvc.CompareAttribute checks if two properties in a Model are equal, but I wanted to implement a validation attribute to check if x < y or x > y, instead of just x == y. All the examples I've found so far on Google show how to implement an attribute for class-level validation, but I haven't yet found any examples that show it for property-level validation.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Re: [MVC3] Implementing a custom validation attribute for CompareTo

    Actually ActionFilters CAN work both on the class level as well as on an individual ActionResult. As long as you don't decorate your ActionFilter with definition to ONLY allow it on the class level.

    Code:
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    Public class CompareTo: ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
             ....your logic goes here for comparing
        }
    }

  3. #3

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [MVC3] Implementing a custom validation attribute for CompareTo

    I found a guide at http://www.devtrends.co.uk/blog/the-...t-mvc-3-part-2 which explains how to implement custom validation attributes for model properties.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width