Reputation: 5594
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?
Upvotes: 2
Views: 4067
Reputation: 1
@Html.TextBoxFor(p => p.DataFim,"{0:d}", new { @class = " datepicker form-control" })
Upvotes: 0
Reputation: 26930
I know mvc 3 is tagged, but mvc 4 has additional parameter "format" for Html.TextBox
Upvotes: 1
Reputation: 82096
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.
Upvotes: 8
Reputation: 103338
<%= Html.TextBoxFor(m => m.Customer.NextEmailDate, new { @Value = m.Customer.NextEmailDate.ToString("dd/MM/yyyy") })%>
Upvotes: 2
Reputation: 67898
Add this annotation to the NextEmailDate
property.
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
Upvotes: 2
Reputation: 8818
If NextEmailDate
is a DateTime
, you could use NextEmailDate.ToShortDateString()
. If isn't isn't a DateTime
, you'll have to tell me what it is.
Upvotes: 2
Reputation: 9519
@Html.TextBoxFor(m=> m.Customer.NextEmailDate.ToString("dd/MM/yyyy"))
Upvotes: 0