Asp.net core mvc 能否在GET视图中使用DataAnnotation.UIHInt

Asp.net core mvc 能否在GET视图中使用DataAnnotation.UIHInt,asp.net-core-mvc,asp.net-core-2.0,Asp.net Core Mvc,Asp.net Core 2.0,System.ComponentModel.DataAnnotations.UIHInt能否在GET视图中使用: 在application.json中指定: { "ApplicationSettings": { "ApplicationName": "My Application" } ... } 应用程序设置类: public class ApplicationSettings { [UIHint("The name of the application dis

System.ComponentModel.DataAnnotations.UIHInt能否在
GET
视图中使用:

application.json
中指定:

{
  "ApplicationSettings": {
    "ApplicationName":  "My Application"
  }
  ...
}
应用程序设置
类:

public class ApplicationSettings
{
    [UIHint("The name of the application displayed in nav bar")]
    public string ApplicationName { get; set; }
}
Startup.cs
中填充:

public void ConfigureServices(IServiceCollection services)
{
  // retrieve the 'ApplicationSettings' section of the appsettings.json file
  var applicationSettings = Configuration.GetSection("ApplicationSettings");
  services.Configure<ApplicationSettings>(applicationSettings);
  ...
}
以HTML格式显示:

ApplicationSettings

ApplicationName: My Application
The name of the application displayed in nav bar

首先,您使用的
UIHint
是错误的。它用于指定
DisplayFor
/
EditorFor
将使用的视图。因此,在这方面,您也可以通过
DisplayFor
将其用于显示

然而,您在这里所做的只是描述这个属性的用途。这是
显示
属性的作业:

[Display(Name = "The name of the application displayed in nav bar")]
然后,您可以执行以下操作:

@Html.DisplayNameFor(x => ApplicationSettings.Value.ApplicationName)
然而,从技术上来说,这也不是你想要的。更有可能的是,您仍然希望显示名称类似于
应用程序名称
,除此之外,它还应该是描述性文本。
Display
属性确实有一个
Description
属性,可用于:

[Display(Name = "Application Name", Description = "The name of the application displayed in nav bar")]
然而,不幸的是,没有内置的方式来实际显示
描述。不过,有一个包含扩展名的

public静态类
{
公共静态IHTML内容描述(此IHTML帮助html,表达式)
{
if(html==null)
抛出新的ArgumentNullException(nameof(html));
if(表达式==null)
抛出新ArgumentNullException(nameof(expression));
var modeleexplorer=ExpressionMetadataProvider.FromLambdaExpression(expression,html.ViewData,html.MetadataProvider);
如果(modelExplorer==null)
抛出新的InvalidOperationException($”无法获取{ExpressionHelper.GetExpressionText(expression)}的模型资源管理器);
返回新的HtmlString(modelExplorer.Metadata.Description);
}
}
礼遇:沃特·休斯中心


首先,您使用的
UIHint
是错误的。它用于指定
DisplayFor
/
EditorFor
将使用的视图。因此,在这方面,您也可以通过
DisplayFor
将其用于显示

然而,您在这里所做的只是描述这个属性的用途。这是
显示
属性的作业:

[Display(Name = "The name of the application displayed in nav bar")]
然后,您可以执行以下操作:

@Html.DisplayNameFor(x => ApplicationSettings.Value.ApplicationName)
然而,从技术上来说,这也不是你想要的。更有可能的是,您仍然希望显示名称类似于
应用程序名称
,除此之外,它还应该是描述性文本。
Display
属性确实有一个
Description
属性,可用于:

[Display(Name = "Application Name", Description = "The name of the application displayed in nav bar")]
然而,不幸的是,没有内置的方式来实际显示
描述。不过,有一个包含扩展名的

public静态类
{
公共静态IHTML内容描述(此IHTML帮助html,表达式)
{
if(html==null)
抛出新的ArgumentNullException(nameof(html));
if(表达式==null)
抛出新ArgumentNullException(nameof(expression));
var modeleexplorer=ExpressionMetadataProvider.FromLambdaExpression(expression,html.ViewData,html.MetadataProvider);
如果(modelExplorer==null)
抛出新的InvalidOperationException($”无法获取{ExpressionHelper.GetExpressionText(expression)}的模型资源管理器);
返回新的HtmlString(modelExplorer.Metadata.Description);
}
}
礼遇:沃特·休斯中心