2

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?

Manta
  • 490
  • 5
  • 18
  • 1
    The code you have shown works fine. Have you included the scripts for client side validation? Is client side validation enabled –  Sep 20 '18 at 21:51
  • Yes js scripts are bundled, client side validation enabled-->confirmed with working other validation attributes – Manta Sep 21 '18 at 09:29
  • 1
    Are there any errors in the browser console when you edit the textbox? –  Sep 21 '18 at 09:34
  • I've just tested, No. I cleaned Visual Studio directories and browser cache with no success...Do you think it could be linked with I don't know, assemblies versions or use of Identity? I really don't understand why it works with a custom validation attribute and not with `[Remote]` – Manta Sep 21 '18 at 09:54
  • 1
    Its client side code :) What is the actual html generate by your `@Html.TextBoxFor(m => m.Name)`? –  Sep 21 '18 at 09:57
  • Post edited with my actual code and HTML generated code – Manta Sep 21 '18 at 10:37
  • 1
    Except for the `htmlAttributes="{ class = form-control }"` - its `@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })` - not, `new { htmlAttributes = new { ... }}`, your code works fine, so if its not working then its due to something your have not shown us –  Sep 21 '18 at 10:41
  • there has to be backend code because your remote attribute is using "httpPostmethod = POST".... hence post.... – Moi Hawk Jan 29 '19 at 02:16
  • Your code doesn't work for me either. This is a krap implementation. It should work as documented here: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-7.0 – pianocomposer Dec 01 '22 at 18:22

3 Answers3

0

Assuming Vehicule is the @model being used in the view

@model Vehicule

then the controller should expected that model

public class DefaultsController : Controller {
    [HttpGet]
    public ActionResult Index() {
        var model = new Vehicule();
        return View(mdoel);
    }

    [HttpPost]
    public ActionResult Index(Vehicule model) {
        if(ModelState.IsValid) {
            //...do something
            //..possible redirect 
        }
        //if we get this far something is wrong with data
        return View(model);
    }
}

The model binder will take validation into account when binding the model from the request.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 1
    No I don't think so, `Model.IsValid` is equal to true because attribute validation isn't done...Even `TestRemoteValidation` is false, the `Model.IsValid` code is performed. – Manta Sep 20 '18 at 14:25
0

add

[AllowAnonymous] to

[HttpPost] public ActionResult TestRemoteValidation(string Name)

Moi Hawk
  • 421
  • 1
  • 5
  • 12
0

Please make sure that you have included below libraries and in correct order in your view

<script src="~/scripts/jquery.js"></script>
<script src="~/scripts/jquery.validate.js"></script>
<script src="~/scripts/jquery.validate.unobtrusive.js"></script>

these libraries are required for remote validation to work.

Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32