5

For my ASP.NET MVC projects, I created a custom validation attribute. Here is the code I am struggling with :

  protected override ValidationResult IsValid(object value, ValidationContext validationContext) {

        //Here I need to resolve the url in order to make a call to that controller action and get the JSON result back

        var httpContext = new HttpContextWrapper(HttpContext.Current);
        var urlHelper = new UrlHelper(
            new System.Web.Routing.RequestContext(
                httpContext, new System.Web.Routing.RouteData()
            )
        );
        var url = urlHelper.Action(Action, Controller, null, 
            urlHelper.RequestContext.HttpContext.Request.Url.Scheme);

        var fullUrl = string.Format("{0}?{1}={2}", url, 
            /*validationContext.MemberName*/"term", value);

        if (!GetResult(fullUrl)) {

            var message = FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(message);
        }

        return null;
    }

You can see the full code from below link :

https://bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/TugberkUg.MVC/Validation/ServerSideRemoteAttribute.cs

For the fullUrl variable, I am trying to append the property name to querystring but when I use validationContext.MemberName, I am failing. I solved the problem with a temp fix by making it static as "term" but it is not a fix at all.

So, what is the way of retrieving property name from validationContext?

2
  • I have the same problem. I even want to go further and get all the CustomAttributes for the property being validated. Sadly the validationContext doesn't contain any useable properties/methods to get this information. Commented Nov 15, 2011 at 12:05
  • This is answered here with more details: stackoverflow.com/questions/7447932/… Commented Oct 10, 2012 at 10:39

1 Answer 1

3

Does validationContext.DisplayName do the trick?

You could then reflect to get the MemberName

var displayName = validationContext.DisplayName;

var memberName = validationContext.ObjectType.GetProperties()
    .Where(p => p.GetCustomAttributes(false).OfType<DisplayAttribute>().Any(a => a.Name == displayName))
    .Select(p => p.Name)
    .FirstOrDefault();

Possibly?

Sign up to request clarification or add additional context in comments.

6 Comments

DisplayName and the MemberName is different.
Yes, you're quite right - in my code I had to choose between being able to access the name of the member (via DisplayName) and having a DisplayAttribute on that member. I'm going to edit my answer with apossible solution though
Sorry, been hectic today - I should be more careful with responding :)
No problem at all :) hmm, I will definately give it a try. you could send a patch if you want as well: bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.