jeudi 2 juin 2016

MVC 3 remote validation not firing for a date inside a Html.BeginCollectionItem block (also tried limit through jQuery datepicker with no luck)

I am trying to implement MVC 3 RemoteValidatorAttribute for the first time. But it is not firing and I don't know what I am doing wrong.

I have a Create view with the following*

@model Demo.Models.Entity
@using Demo.Models;
...
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
...
@using (Html.BeginForm("Create", "Entities", FormMethod.Post, new { enctype = "multipart/form-data", role = "form" })) 
{
    @Html.ValidationSummary(false)
    ...
    <table>
        <tr class="editor-table-detail-row">
            <th>Purchase Date</th>
        </tr>
        @foreach(EntityDetail detailItem in Model.details)
        {
            Html.RenderPartial("_DetailEditorRow", detailItem);
        }
    </table>
    ...
}
...

The partial view code is as follows*

@model Demo.Models.EntityDetail

<tr class="editor-table-detail-row">
@using (Html.BeginCollectionItem("details"))
{
    <td>
        @Html.EditorFor(model => model.purchaseDate, "BootstrapDefaultDatePicker")
        @Html.ValidationMessageFor(model => model.purchaseDate)
    </td>
    <td>
        <a href="#" class="delete-table-row">Delete</a>
    </td>
}
</tr>

I create a ValidationController as follows*

using System;
using System.Web.Mvc;

namespace PostalRepairs.Controllers
{
    public class ValidationController : Controller
    {
        [HttpPost]
        public JsonResult ValidateDateNotFuture(DateTime date)
        {
            if (date <= DateTime.Now) 
                return Json(true);

            return Json(false);    
        }
    }
}

And the EntityDetailClass is as follows * using System; using System.ComponentModel.DataAnnotations; using System.Collections.Generic; using System.Web.Mvc;

namespace Demo.Models
{
    public class EntityDetail 
    {
        ...
        [Display(Name = "Purchase Date")]
        [Remote("ValidateDateNotFuture", "Validation", HttpMethod = "Post", ErrorMessage = "The {0} cannot be in the future.")]
        [Required]
        public DateTime? purchaseDate { get; set; }
        ...        
    }
}

*I have removed the lines not involved in the behavior I am trying to reproduce and change model, controller, view and attribute name for confidential issues. The only real names are EntityDetail.purchaseDate and all the code in ValidationController class.

But when I test the solution the purchase date is accepting invalid dates (according to business rules, purchase date should not be in the future). If a place a breakpoint inside ValidateDateNotFuture controller action method the break point never stops execution so I am guessing the action is never being called.

Can anyone give me some lights?

BTW I was trying to limit the purchase date datepicker but this jquery code is not working. If I can limit the input then the remote validator would not be necessary. I am using a bootstrap template in this project

$("#add-details-table-item").click(function () {
    $.ajax({
        url: this.href,
        cache: false,
        success: function (html) {
            $("tr.editor-table-detail-row:last").after(html);
            $("tr.editor-table-detail-row:last").find(".default-date-picker").datepicker({
                maxDate: '+0'
            });
        }
    });
    return false;
});

$("tr.editor-table-detail-row .default-date-picker").each(function () {
    $(this).datepicker({
        maxDate: '+0'
    });
});

I have tried with .datepicker({ maxDate: 0 }); .datepicker({ maxDate: '0' }); .datepicker({ maxDate: '+0' }); .datepicker({ maxDate: $.now() });

And no luck.

Aucun commentaire:

Enregistrer un commentaire