使用 Display 属性而不是 DisplayName,如下所示:
[Display(Name = "Page Url", Description = "Enter page Url")]
public string Url { get; set; }
然后,创建这个 HtmlHelper:
public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var description = metadata.Description;
TagBuilder tagBuilder = new TagBuilder("span");
tagBuilder.SetInnerText(description);
return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}
并且,像这样在您的视图中使用它(用于描述):
@Html.DescriptionFor(model => model.Url)
并且,对于名称:
@Html.LabelFor(model => model.Url)