Display validation message from IValidatableObject interface?

I have a model which contains two property of datetime type of nullable type. I am trying to display error message if the user has given StartWorkDate and EndWorkDate.

I am not able to display the message in view where i am using Ajax.BeginForm.

I can’t apply [Required] attribute as it optional.

public class work:IValidatableObject
{
   public DateTime? StartWorkDate{get;set;}
   public DateTime? EndWorkDate{get;set;}

   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> errormsg = new List<ValidationResult>();
            if(StartWorkDate!=null && EndWorkDate!=null )
            {
                if (EndWorkDate< StartWorkDate)
                {
                    errormsg.Add(new ValidationResult("EndWorkDate must be greater than StartWorkDate", new[] { "StartWorkDate" }));   
                }
            }
            return errormsg ;
        }
}

What is the problem here? it seems to be working fine

public class work : IValidatableObject
    {
        [DataType(DataType.Date)]
        public DateTime? StartWorkDate { get; set; }
        [DataType(DataType.Date)]
        public DateTime? EndWorkDate { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> errormsg = new List<ValidationResult>();
            if (StartWorkDate.HasValue && EndWorkDate.HasValue)
            {
                if (EndWorkDate.Value.Date < StartWorkDate.Value.Date)
                {
                    errormsg.Add(new ValidationResult("EndWorkDate must be greater than StartWorkDate", new[] { "EndWorkDate" }));
                }
            }
            return errormsg;
        }
    }

Can you please post your view code so that i can understand where am i going wrong?

I am using Ajax.Begin form and i am display a partial view inside the UpdateTargetId of Ajax.BeginForm.

If possible please post your View Code so i have a look at it.

Thanks.

Model

public class work : IValidatableObject
    {
        [Display(Name = "start date")]
        [DataType(DataType.Date)]
        public DateTime? StartWorkDate { get; set; }
        [DataType(DataType.Date)]
        [Display(Name="end date")]
        public DateTime? EndWorkDate { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {

            List<ValidationResult> errormsg = new List<ValidationResult>();
            if (StartWorkDate.HasValue && EndWorkDate.HasValue)
            {
                if (EndWorkDate.Value.Date < StartWorkDate.Value.Date)
                {
                    
                    errormsg.Add(new ValidationResult("EndWorkDate must be greater than StartWorkDate", new[] { "EndWorkDate" }));
                }
            }
            return errormsg;
        }
    }

View

@model work

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

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Job</h4>
        <hr />
        @Html.ValidationSummary(true)
        <span id="mes"></span>
        <div class="form-group">
            @Html.LabelFor(model => model.StartWorkDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StartWorkDate)
                @Html.ValidationMessageFor(model => model.StartWorkDate)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EndWorkDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EndWorkDate)
                @Html.ValidationMessageFor(model => model.EndWorkDate)
            </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>
    
}

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

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

Leave a Reply