【问题标题】:asp.net mvc 5 html.DisplayFor DisplayFormat Display(Name) not workingasp.net mvc 5 html.DisplayFor DisplayFormat 显示(名称)不起作用
【发布时间】:2015-06-29 16:18:45
【问题描述】:

Html.DisplayFor 停止显示显示名称和格式化字符串...我的代码如下...有什么想法吗?我已经搜索了任何找不到任何东西。谢谢

[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; }

剃刀.cshtml:

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

...
    @foreach (var item in Model)
    {
...
            <td>
            @Html.DisplayFor(modelItem => item.EventDateTimeDay)
            </td>

【问题讨论】:

    标签: asp.net-mvc razor


    【解决方案1】:

    您应该尝试使用 DisplayName 属性

        [DisplayName("Seconds")]
        public double Duration { get; set; }
    

    【讨论】:

    • 是的,也试过了,但还是不行。感谢您的回复。并且它没有解释为什么 DisplayFormat 不起作用。
    • 看起来这个问题已经在另一个帖子中得到了回答[link]stackoverflow.com/questions/15976576/…
    【解决方案2】:

    找出问题所在。我使用 Unity 作为 IOC 并减少统一解决错误,我在 UnityConfig 中添加了这个:

    container.RegisterType<ModelMetadataProvider, EmptyModelMetadataProvider>();
    

    它清除了数据属性。把它改成这个,它工作得很好:

    container.RegisterInstance<ModelMetadataProvider>(ModelMetadataProviders.Current);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      相关资源
      最近更新 更多