Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Short Date string in html textbox mvc

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?

like image 846
CR41G14 Avatar asked Feb 16 '26 13:02

CR41G14


1 Answers

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.

like image 122
James Avatar answered Feb 19 '26 02:02

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!