How to configure actions if I specify AdditionalFields
for validation via RemoteAttribute to handle possible optional additional fields? Currently I try to have actions for possible combinations of optional fields and it does not work as show below.
I have a form that is bound to a model and one of the fields is a remote validation field for the username. When I run the program, I am getting an error. Here is my validation code:
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class ValidationController : Controller
{
public JsonResult IsUserNameAvailable(string userName)
{
// compute condition, only "fail" shown.
return Json(string.Format("{0} is already taken.", userName),
JsonRequestBehavior.AllowGet);
}
public JsonResult IsUserNameAvailable(string userName, string UserId)
{
// compute condition using both name and ID, only "fail" shown.
return Json(string.Format("{0} is already taken.", userName),
JsonRequestBehavior.AllowGet);
}
}
Here is my model:
public class EditUserAdministrationViewModel
{
public int UserId { get; set; }
[Required(ErrorMessage = "You must enter a first name.")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "You must enter a user name.")]
[Display(Name = "User Name")]
[Remote("IsUserNameAvailable", "Validation", AdditionalFields = "UserId")]
public string UserName { get; set; }
// more fileds
}
Indeed I get the runtime error (since there are 2 actions matching by name as covered in Routing: The current request for action [...] is ambiguous between the following action methods):
The current request for action 'IsUserNameAvailable' on controller type 'ValidationController' is ambiguous between the following action methods:
System.Web.Mvc.JsonResult IsUserNameAvailable(System.String) on type BdsManager.Controllers.ValidationController
System.Web.Mvc.JsonResult IsUserNameAvailable(System.String, System.String) on type BdsManager.Controllers.ValidationController