I have the following model . How can I store the datetime value which are stored in the column MOTDate , TaxDate into the column MOTDateString, TaxDateString automatically.
public class Vehicles
{
public DateTime? MOTDate { get; set; }
public DateTime? TaxDate { get; set; }
public DateTime? RegDate { get; set; }
public DateTime? InsuredDate { get; set; }
[NotMapped]
public string MOTDateString {get;set;}
[NotMapped]
public string TaxDateString { get; set; }
}
Is there anything wrong?As mgebhard said,it could set the MOTDateString, TaxDateString automatically which depends on the value of MOTDate , TaxDate.
Model:
public class Vehicles
{
public DateTime? MOTDate { get; set; }
public DateTime? TaxDate { get; set; }
public DateTime? RegDate { get; set; }
public DateTime? InsuredDate { get; set; }
[NotMapped]
public string MOTDateString
{
get
{
return MOTDate?.ToString();
}
}
[NotMapped]
public string TaxDateString
{
get
{
return TaxDate?.ToString();
}
}
}
Controller(For easy testing,I just set the value manually):
var data = new Vehicles()
{
MOTDate = DateTime.Parse("2019-8-7"),
TaxDate = DateTime.Parse("2020-7-7")
};
Result:
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
411 Points
1327 Posts
how can I assign the datetime value into string variable in model class
Jun 17, 2020 08:11 AM|polachan|LINK
I have the following model . How can I store the datetime value which are stored in the column MOTDate , TaxDate into the column MOTDateString, TaxDateString automatically.
All-Star
53041 Points
23612 Posts
Re: how can I assign the datetime value into string variable in model class
Jun 17, 2020 11:00 AM|mgebhard|LINK
Pretty simple but confusing why you ask this question over and over.
Contributor
2690 Points
874 Posts
Re: how can I assign the datetime value into string variable in model class
Jun 19, 2020 09:51 AM|Rena Ni|LINK
Hi polachan,
Is there anything wrong?As mgebhard said,it could set the MOTDateString, TaxDateString automatically which depends on the value of MOTDate , TaxDate.
Model:
Controller(For easy testing,I just set the value manually):
Result:
Best Regards,
Rena