6

I have a View on which I need to display a date formatted in "dd/MM/yyyy".

Actually it is displayed as: @Html.LabelFor(model => model.DtNews) and I don't know where and how I can put the Format() function.

Data is retrieved from the database using EF. How can I do it?

6 Answers 6

7
@Html.LabelFor(model => model.DtNews)
@Html.EditorFor(model => model.DtNews)

and on your view model you could use the [DisplayFormat] attribute

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DtNews { get; set; }

Now you're gonna tell me that this class is generated by the EF framework to which I would respond to you: YOU SHOULD NOT USE YOUR EF AUTOGENERATED MODELS IN YOUR VIEWS. You should define and use view models. The view models are classes specifically tailored to the requirements of your views. For example in this particular view you have a requirement to format the dates in a particular way: perfect fit for view models:

public class MyViewModel
{
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime DtNews { get; set; }
}

then your controller action could query your repository and fetch the domain model (EF autogenerated entity) and map it to a view model. Then it will pass this view model to the view.

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

3 Comments

This field comes from EF: do i need to create a ViewModel for that?
@Cris, in a correctly architected ASP.NET MVC application you should create a view model for each view. This way you are totally independent from where this data comes. Today it's some EFs generated models, tomorrow an XML file and the day after tommorow your Facebook account. In order to make your ASP.NET MVC application as loosely coupled to the origin of your data you should always use view models. ALWAYS.
^ agreed - view models are definitely the way to go.
1

I'd just throw a buddy class on your model.DtNews

A buddy class will decorate your existing model

[MetadataType(NewsMetadata)]
public partial class News // this is the same name as the News model from EF
{ /* ... */ }


/* Metadata type */
public class NewsMetadata
{
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime DtNews { get; set; }
}

2 Comments

This field is generated by Entity Framework, where should i put this property?
you'll create a buddy class to achieve this - stackoverflow.com/questions/1245529/…
1

Try this. it work for me.

@Model.DtNews.Value.ToString("dd-MM-yyyy")

Comments

0

@Html.LabelFor(model => model.DtNews.ToString("dd/MM/yyyy"))

^should do the trick.

you could also use editor/display templates as discussed here.

Comments

0

If DtNews is a DateTime, then try this:

@Html.LabelFor(model => model.DtNews.ToString("dd/MM/yyyy"));

Comments

0

Use This

@Html.TextBoxFor(m => m.MktEnquiryDetail.CallbackDate, "{0:dd/MM/yyyy}")

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.