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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Cris
  • 12,124
  • 27
  • 92
  • 159

6 Answers6

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This field comes from EF: do i need to create a ViewModel for that? – Cris Jul 11 '11 at 20:42
  • 1
    @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**. – Darin Dimitrov Jul 11 '11 at 20:43
  • ^ agreed - view models are definitely the way to go. – Chase Florell Jul 11 '11 at 20:48
1

Try this. it work for me.

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

BJ Patel
  • 6,148
  • 11
  • 47
  • 81
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; }
}
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
0

Use This

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

D Mishra
  • 1,518
  • 1
  • 13
  • 17
0

If DtNews is a DateTime, then try this:

@Html.LabelFor(model => model.DtNews.ToString("dd/MM/yyyy"));
Conner
  • 30,144
  • 8
  • 52
  • 73
Mrchief
  • 75,126
  • 20
  • 142
  • 189
0

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

^should do the trick.

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

Community
  • 1
  • 1
David Wick
  • 7,055
  • 2
  • 36
  • 38