I have MVC app with a custom login page. The model is generated by Entity Framework and it's called "User" . When use logs in, the server code updates the login date: user.LoginDate = DateTime.Now; And it works fine.
But now I started applying the validation. What I did is: 1) I created another class, made it partial and named User.cs:
[MetadataType(typeof(UserMetadata))]
public partial class User
{
[Bind]
public class UserMetadata
{
[DisplayName("Email")]
[Required(ErrorMessage="Email field is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
[StringLength(30)]
public string UserEmail { get; set; }
...
[Range(typeof(DateTime), "1/1/2010", "2/2/2024", ErrorMessage = "message")]
public Nullable<System.DateTime> LoginDate { get; set; }
}
}
And now I have a problem. When EF tries to update the database, it throws an error about some kinda data validation issue in the code where I'm trying to update the LoginDate field.
If I comment this class, it works fine, no error, but no validation neither.
I thought that maybe it's datetime format, so I also tried the following attribute for a LoginDate field:
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{yyyy/MM/dd HH:mm:ss tt}")]
Also tried [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{yyyy/MM/dd HH:mm:ss.fff}")]
However, it did not help and it's quite frustrating, because when I look at my database, I see that the LoginDate is written in the proper format: 2014-12-26 19:49:12.247
Please advise.
LoginDate
? This field is not user's input. Your own server code assigns correct values to it. – Mark Shevchenko