Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DataType.Date
Message
 
 
To
31/08/2012 10:52:15
General information
Forum:
ASP.NET
Category:
MVC
Title:
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01551951
Message ID:
01552149
Views:
53
>I might be able to get a working example this weekend.
>
>

It looks like IE 9 has a bug for certain cases. JQuery lists it as closed "worksforme", however, it doesn't work for me.

Also, how can I create a custom annotation attribute? I want to set the dates to be future only.

I can do it this way:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace Socrates.Objects
{
    public class Engagement:IValidatableObject
    {
        public virtual int Id { get; set; }
        
        [Required]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        [DisplayName("Start Date")]
       [DataType(DataType.Date)]
        public virtual DateTime? StartDate { get; set; }

        [Required]
        [DataType(DataType.Date)]
        [DisplayName("End Date")]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public virtual DateTime? EndDate { get; set; }

        public virtual Client Client { get; set; }
        public virtual Course Course { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            //string[] memberNames = new string[] { validationContext.MemberName };

            if ((EndDate==null) || (StartDate == null))
            {
                yield return new ValidationResult("StartDate or EndDate is null - something went terribly wrong!");
            }
            
            
            if (EndDate < StartDate)
            {
                yield return new ValidationResult("Class cannot end before it starts", new[]{"Engagement.EndDate"});
            }

            if (StartDate < DateTime.Now.Date)
            {
                yield return new ValidationResult("Start Date can not be in the past", new[] {"Engagement.StartDate"});
            }


            if (EndDate > StartDate.Value.AddMonths(1))
            {
                yield return new ValidationResult("You can not have a class longer than a month", new[]{"Engagement.EndDate"});
            }
        }


    }
}
but then the validation happen only after I press Create button. I want them to be right away.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform