I'm working in my ASP.NET Core project and trying to add current registration date and time when a new user is registered. I used the following codes for this purpose. This is a part of registration ActionResult:
DateTime currentDate = DateTime.Now;
var user = new AppUser() {
department = registerModel.department,
creationDate = registerModel.creationDate = currentDate,
isAdmin = registerModel.isAdmin,
isManager = registerModel.isManager,
isActive = registerModel.isActive,
canEdit = registerModel.canEdit,
canDelete = registerModel.canDelete,
canSendMessage = registerModel.canSendMessage,
canSeeNotification = registerModel.canSeeNotification
};
When I press the register button, the following error is shown:
An unhandled exception occurred while processing the request. InvalidCastException: The field of type System.DateTime must be a string, array or ICollection type. System.ComponentModel.DataAnnotations.MaxLengthAttribute.IsValid(object value)
How can I produce the format SQL Server accepts while keeping that object as DateTime object?
Updated:
public class AppUser: IdentityUser < int > {
[Display(Name = "Department")]
[Required(ErrorMessage = "Please enter depertment")]
[MaxLength(20)]
public string department {
get;
set;
}
[Display(Name = "Creation date")]
[DataType(DataType.DateTime)]
[MaxLength(100)]
public DateTime creationDate {
get;
set;
}
[Required]
//[Range(0, 1)]
public bool isAdmin {
get;
set;
}
[Required]
//[Range(0, 1)]
public bool isManager {
get;
set;
}
[Required]
//[Range(0, 1)]
public bool isActive {
get;
set;
}
[Required]
//[Range(0, 1)]
public bool canEdit {
get;
set;
}
[Required]
//[Range(0, 1)]
public bool canDelete {
get;
set;
}
[Required]
//[Range(0, 1)]
public bool canSendMessage {
get;
set;
}
[Required]
//[Range(0, 1)]
public bool canSeeNotification {
get;
set;
}
CodePudding user response:
try this
creationDate = currentDate,
isAdmin = registerModel.isAdmin,
...