【问题标题】:Data Annotations for Display Name are not rendering in Razor form显示名称的数据注释未以 Razor 形式呈现
【发布时间】:2020-03-22 05:07:04
【问题描述】:

我正在尝试使用数据注释在我的模型类中分配一个显示名称,但视图没有显示该名称。它默认为属性名称并忽略数据注释。

在我的模型中:

public interface IFeedbackLogMetadata
{
        [Display(Name="Description")]
        string FeedbackDescription { get; set; }
}
public partial class FeedbackLog : PersistantEntity, IFeedbackLogMetadata
{
//..other stuff
}

我也试过了:

[DisplayName("Description")]
string FeedbackDescription { get; set; }

在我看来,我已经尝试过:

@Html.LabelFor(model => model.FeedbackDescription, htmlAttributes: new {@class = "control-label col-md-2"})
<label class="control-label col-md-2">@Html.DisplayNameFor(d => d.FeedbackDescription)</label>

标签显示“FeedbackDescription”。它应该是“描述”。为什么显示名称数据注释被忽略?

【问题讨论】:

  • 确保没有Views/Shared/DisplayTemplates 用于覆盖它的字符串。
  • @SteveGreene - 我不知道。

标签: asp.net-mvc razor data-annotations


【解决方案1】:

/头晕/

我需要用接口的 MetadataType 来装饰部分类。如果没有这个,视图将从 EF 生成的类中提取,该类没有数据注释。

[MetadataType(typeof(IFeedbackLogMetadata))]
public partial class FeedbackLog : PersistantEntity, IFeedbackLogMetadata
{
//...other stuff
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2016-04-17
    • 1970-01-01
    • 2014-05-23
    • 2017-12-29
    相关资源
    最近更新 更多