Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:38:13+00:00 2026-05-24T08:38:13+00:00

I have a ViewModel with a StartDate and an EndDate. Obviously, I need to

  • 0

I have a ViewModel with a StartDate and an EndDate. Obviously, I need to validate StartDate <= EndDate.

I created the StartDateBeforeEndDate validation attribute and decorated the ViewModel class with it:

public class ValidProgramDisplayStartDateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        ProgramCreateOrEditViewModel vm = value as ProgramCreateOrEditViewModel;
        if (vm == null) return true; //not our problem

        if (!vm.EndDisplay.HasValue) return true; 
            //if you don't set an end date, you can't be invalid

        return vm.StartDisplay <= vm.EndDisplay;
    }
}

…

[ValidProgramDisplayStartDate(ErrorMessage="The program start display date cannot be after the program display end date.")]
public class ProgramCreateOrEditViewModel
{

    [Required(ErrorMessage = "A program title is required.")]
    [StringLength(255, ErrorMessage = "The program title cannot exceed 255 characters.")]
    public virtual string Title { get; set; }

    private DateTime _startDisplayDate = DateTime.Now;

    [Display(Name = "Display Start Date")]
    [Required(ErrorMessage = "A start display date is required")]
    [DataType(DataType.Date)]
    public virtual DateTime StartDisplay { get { return _startDisplayDate; } set { _startDisplayDate = value; } }

    [Display(Name="Display End Date")]
    [DataType(DataType.Date)]
    public virtual DateTime? EndDisplay { get; set; }

...more properties omitted...
}

The Title is correctly validated and its validation message appears in the validation summary as expected, but a breakpoint on IsValid never fires, and no validation error appears if you give an invalid pairing.

The ValidationAttribute works correctly in a unit test that creates a ViewModel object, populates the dates, creates the ValidationAttribute object and calls its IsValid method. It also works correctly if I set a breakpoint on the POST action in the Controller, and use Visual Studio’s immediate window to construct the ValidationAttribute and pass it the received ViewModel.

I don’t really care about client side validation at this stage; that’s filed under “nice to have but who’s got the time”? The server-side validation however, is essential.

View:

@model MyProject.Web.Mvc.Controllers.ViewModels.ProgramCreateOrEditViewModel
@using MvcContrib.FluentHtml
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm())
{

<fieldset class="program"><legend>Program</legend>
    @if (!ViewData.ModelState.IsValid)
    {
        <p class="error">Please correct all errors listed below. The program cannot be saved while errors remain.</p>
        @Html.ValidationSummary(false)
    }
    @Html.HiddenFor(m => m.Id)
    <h3>
        @Html.LabelFor(m => m.Title)
        @Html.TextBoxFor(m => m.Title)</h3>
    <div id="accordion">
        <h3><a href="#">Dates:</a></h3>
        <div class="section">
            Dates related to <em>displaying</em> the program on our site:<br />
            @Html.LabelFor(m => m.StartDisplay)
            @Html.EditorFor(m => m.StartDisplay, new { _class = "date" })
            @Html.LabelFor(m => m.EndDisplay)
            @Html.EditorFor(m => m.EndDisplay, new { _class = "date enddisplay" })
        </div>
     ... more form fields omitted ...
    </div>
   <p>
        <input type="submit" value="Save" /></p>
</fieldset>
}

And finally, the editor template:

@model DateTime?
@Html.TextBox("", Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : String.Empty, new { @class = "date" })
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:38:13+00:00Added an answer on May 24, 2026 at 8:38 am

    The issue appears to be that a class ValidationAttribute will not be processed if there are any model errors in the class properties. It will only be processed after the ValidationAttributes within the class are all satisfied.

    For your purposes, if you move [ValidProgramDisplayStartDate(ErrorMessage="The program start display date cannot be after the program display end date.")] from decorating the class to decorating your Id property, then you will get your server side validation, even if there are other model errors.

    counsellorben

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a viewmodel that contains a product and SelectList of categories. public class
If I have a ViewModel like so: public class ViewModel : IPropertyChangeBase, IViewModel {
I have a ViewModel class FontsViewModel : ObservableObject { public FontsViewModel() { InitFonts(); }
Suppose I have ViewModel like public class AnotherViewModel { public string Name { get;
I have created a viewmodel class for my custom view template. Right now I
I have ViewModel class as follows: public class ListViewModel { public ObservableCollection<InfoItem> List {
I have a viewmodel defined as public class linkVM { public AL.Common.Content.Object.PageIndex[] pages{ get;
I have following ViewModel containing NumberOfAdults fields. public class SearchView { public int NumberOfAdults
I have viewmodel which i am contructing in a Create get action, public class
I have a ViewModel. something like this public class ViewModel { public int Id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.