Search code examples
asp.netasp.net-mvcasp.net-mvc-validationremote-validation

ASP MVC Remote Validation Removing Area


I have the following Model:

public class UserModel 
{
   [Required]
   public String Name {get; set; }

   [Required]
   public String Surname {get; set; }

   [Required]
   [Remote("ValidateIdNumber", "CustomValidation", "", ErrorMessage = "Invalid ID")]
   public String IDNumber {get; set; }
}

I have a create user form that is the Admin area. So it's "/Admin/User/Create".

My "ValidateIdNumber" action is in the root in the controller "CustomValidation" So to access it you would need to go to "/CustomValidation/ValidateIdNumber"

public Boolean ValidateIdNumber(String IDNumber) {
     //Validate ID number and return result.
}

The problem is that when the form gets created, the remote validation targets "/Admin/CustomValidation/ValidateIdNumber".

<input class="text-box single-line" data-val="true" data-val-remote="Invalid ID" data-val-remote-additionalfields="*.IDNumber" data-val-remote-url="/Admin/CustomValidation/ValidateIdNumber" data-val-required="Please provide a ID Number" id="IDNumber" name="IDNumber" type="text" value="">

How can I remove the Area? I've tried making the area parameter in the Remote attribute an empty string("") and null.

any ideas? I want to try and keep my validation in the root and not in areas.


Solution

  • You can write a custom remote attribute , see here:

    ASP.NET MVC 3 Remote Validation