I have a model as Follows, I am using Linq to SQL
public class CustomerDetails
{
public Customer customer { get; set; }
}
"Customer" is populated from a single customer from the database. Customer has a field "NextEmailDate". I want to bind this to a textBox but only show the date and not the time
@Html.TextBoxFor(m=> m.Customer.NextEmailDate)
But I get a text box with the text 16/09/2012 00:00:00 how do I make this just 16/09/2012?
Assuming Customer is your actual entity, introduce a view model and pass that to your view instead. That way you can decorate it with all the formatting/validation attributes you need e.g.
public class CustomerViewModel
{
[Required(ErrorMessage = "Next email date is required!")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}"]
public DateTime NextEmailDate { get; set; }
...
}
public class CustomerDetails
{
public CustomerViewModel Customer { get; set; }
}
You can map the properties manually or use a library like AutoMapper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With