1

Html.DisplayFor stopped displaying the display name and the formatted string... My code is bellow ... any ideas? i have searched around any couldn't find anything. thanks

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
[Display(Name = "Date")]
public DateTime EventDateTimeDay { get; set; }

[Display(Name = "Seconds")]
[DisplayFormat(DataFormatString = "{0:0.00} s")]
public double Duration { get; set; }

Razor .cshtml:

@model IEnumerable<OurClass>
...
        <th>
            @Html.DisplayNameFor(model => model.EventDateTimeDay)
        </th>

...
    @foreach (var item in Model)
    {
...
            <td>
            @Html.DisplayFor(modelItem => item.EventDateTimeDay)
            </td>
DasDas
  • 571
  • 3
  • 9
  • 32
dan
  • 801
  • 15
  • 41

2 Answers2

0

You should try using the DisplayName attribute

    [DisplayName("Seconds")]
    public double Duration { get; set; }
  • yes tried that too, that does not work. thanks for responding tho. and it doesn't explain why the DisplayFormat doesn't work. – dan Jul 01 '15 at 02:43
  • Looks like this has been answered in another post [link]http://stackoverflow.com/questions/15976576/mvc-and-entity-framework-html-displaynamefor-with-composite-viewmodel[/link] – user1440815 Jul 01 '15 at 15:18
0

Figured out the problem. Am using Unity as an IOC and to reduce unity resolve errors I added this in the UnityConfig:

container.RegisterType<ModelMetadataProvider, EmptyModelMetadataProvider>();

which wiped out the data attributes. changed it to this and it worked fine:

container.RegisterInstance<ModelMetadataProvider>(ModelMetadataProviders.Current);
dan
  • 801
  • 15
  • 41