2

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?

7 Answers 7

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@james - Good Solution. But How can I add a Validation to it. For range of DateTime.
2

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.

3 Comments

I don't understand why others are better since yours take into account (eventually) localization... you got my upvote!
@Tallmaris true that. Sometimes I take that for granted. Thanks for the vote!
I use this way. Easier to change the format used on the page too, won't require the source to be compiled again.
2

Add this annotation to the NextEmailDate property.

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] 

7 Comments

Probably dd/MM/yyyy for 16/09, but close enough. :)
@Mike NextEmailDate property isn't in the model though, its just part of the Customer object. How would this change the model?
@Curt, adding this annotation to that property in the Customer object will cause MVC to use the format when sending that property to the TextBoxFor extension method.
@Mike What if another property is in the Customer object which doesn't require this format, such as FirstName? Will this be affected?
@Curt - DataAnnotations are applied per property so in this case the format is only applied to the NextEmailDate.
|
2
<%= Html.TextBoxFor(m => m.Customer.NextEmailDate, new { @Value = m.Customer.NextEmailDate.ToString("dd/MM/yyyy") })%>

Comments

1

I know mvc 3 is tagged, but mvc 4 has additional parameter "format" for Html.TextBox

Comments

0
@Html.TextBoxFor(m=> m.Customer.NextEmailDate.ToString("dd/MM/yyyy"))

Comments

0
@Html.TextBoxFor(p => p.DataFim,"{0:d}", new { @class = " datepicker form-control" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.