Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Validation : get the property name from validationContext
    primarykey
    data
    text
    <p>For my ASP.NET MVC projects, I created a custom validation attribute. Here is the code I am struggling with :</p> <pre><code> 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; } </code></pre> <p>You can see the full code from below link :</p> <p><a href="https://bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/TugberkUg.MVC/Validation/ServerSideRemoteAttribute.cs" rel="nofollow">https://bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/TugberkUg.MVC/Validation/ServerSideRemoteAttribute.cs</a></p> <p>For the <code>fullUrl</code> variable, I am trying to append the property name to querystring but when I use <code>validationContext.MemberName</code>, 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. </p> <p>So, what is the way of retrieving property name from <code>validationContext</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload