C# MVC próba użycia własnego walidatora IValidatableObject, walidator wygląda na martwy

0

Cześć,

stworzyłem prosty projekt na ten cel, próbuję porównać dwie daty, jeśli jedna jest większa od drugiej to ma wyrzucić komunikat.
Próbuję się posłużyć

 IValidatableObject

i skorzystać z metody Validate

Ale żadnego komunikatu błędu nie ma, a modelstate w kontrolerze wchodzi jako poprawny...
Poniżej copy/paste najważniejszych fragmentów. W zasadzie skupić się można jedynie na klasie modelowej, widok był generowany automatem z metody w kontrolerze po podaniu modelu klasy DateRange. W widoku dodałem jedynie datepicker (kalendarz) ale to tutaj nie jest ważne.

Dzięki za pomoc!!!

Model
```csharp
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace DateValidationTest.Models
{
    public class DateRange : IValidatableObject
    {
        [Required]
        [DataType(DataType.Date)]
        public DateTime DateA { get; set; }

        [Required]
        [DataType(DataType.Date)]
        public DateTime DateB { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> result = new List<ValidationResult>();

            if (DateA > DateB) 
            {
                result.Add(new ValidationResult("DateA > DateB")); 
            }

            return result;
        }



    }
} 

Widok

@model DateValidationTest.Models.DateRange

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>DateRange</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.DateA, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DateA, new { htmlAttributes = new { @class = "form-control datepicker" } })
                @Html.ValidationMessageFor(model => model.DateA, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DateB, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DateB, new { htmlAttributes = new { @class = "form-control datepicker" } })
                @Html.ValidationMessageFor(model => model.DateB, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<script>
    $('.datepicker').datepicker({
        format: "yyyy-mm-dd",
        todayBtn: "linked",
        language: "pl",
        multidateSeparator: "-",
        calendarWeeks: true,
        autoclose: true,
        todayHighlight: true
    });
</script>

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
} 

Kontroler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DateValidationTest.Controllers
{
    public class TestController : Controller
    {
        // GET: Test
        public ActionResult Index()
        {
            return View();
        }

        // POST
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            if (ModelState.IsValid) 
            {
                return View(); 
            }
            else
            {
                return View(); 
            }
            
        }

 
    }
}
 
1

Sam sobie pomogłem. Odpowiedź.

Kontroler

(...)
        // POST
        [HttpPost]
        public ActionResult Index(DateRange Model)

(...)
 

1 użytkowników online, w tym zalogowanych: 0, gości: 1