I'm trying to implement remote validation in MVC. I have read several tutos and questions already posted here, but there is no answer.
Controller :
public class GroupsController: Controller
{
[HttpPost]
public ActionResult TestRemoteValidation(string Name)
{
return Json(false);
}
}
View :
@using (Html.BeginForm("Index", "Defaults", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
<input type="submit" class="btn btn-primary" value="Enregistrer" />
}
And Model :
public class Group
{
[Key]
public int Id { get; set; }
[Display(Name = "Nom du Groupe")]
[Required]
[Remote("TestRemoteValidation", "Groups", HttpMethod = "POST", ErrorMessage = "Remote fired")]
//[CustomRemoteValidation("TestRemoteValidation", "Groups", AdditionalFields = "Id")]
public string Name { get; set; }
public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
}
Generated HTML Code:
<input data-val="true" data-val-remote="Remote fired" data-val-remote-additionalfields="*.Name" data-val-remote-type="POST" data-val-remote-url="/Groups/TestRemoteValidation" data-val-required="Le champ Nom du Groupe est requis." htmlAttributes="{ class = form-control }" id="Name" name="Name" type="text" value="" />
I'm using Metadata
because it's an entity-->not the problem, I checked with an other ViewModel and it's the same.
[Required]
and [StringLength(10)]
are fired.
When I put a breakpoint in TestRemoteValidation
, nothing happens.
For instance I'm able to perform the remote validation with a custom remote attribute class and Model.IsValid
override, but I don't understand why this way doesn't work. Any idea?