I have 2 classes containg POCO entities in MVC3
public class TeamMember
{
public DateTime JoinDate { get; set; }
[Required]
Public string Name{ get; set; }
}
public class Project
{
[Required]
public string Name { get; set; }
public DateTime StartDate { get; set; }
}
I want to set the default value of TeamMember JoinDate with Project StartDate.
Can anybody pull me out of this
You could do it in the controller, something like:
and you could add more logic if
project.StartDate == nullthenproject.StartDate = DateTime.Now.I’m not sure you can do this as a default in the model itself though.
Update
As you have it, I don’t think the
Project StartDatecan see theTeamMember JoinDate, I think you will need to give them a one to one relationship. I’m not sure of the exact syntax since I’m not on my work system, but something like this should work:Update 2
Thinking about it, you need to allow JoinDate to be null or else it will throw a validation error, then you need to check to see if there is a value for it. Something more like this: