System.ComponentModel.DataAnnotations.UIHInt能否在GET视图中使用:
在application.json中指定
{
"ApplicationSettings": {
"ApplicationName": "My Application"
}
...
}ApplicationSettings类:
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);
...
}(以某种方式)添加到/Home/About
<h3>ApplicationSettings</h3>
<div>
<dl class="dl-horizontal">
<dt>ApplicationName</dt>: <dd>@ApplicationSettings.Value.ApplicationName</dd>
<????>
...
</div>在HTML中显示:
ApplicationSettings
ApplicationName: My Application
The name of the application displayed in nav bar发布于 2018-02-02 14:11:29
首先,您使用UIHint是错误的。它用于指定将由DisplayFor/EditorFor使用的视图。因此,在这方面,是的,您也可以使用它作为显示,通过DisplayFor。
但是,您在这里所做的只是描述这个属性的用途。这是Display属性的一个作业:
[Display(Name = "The name of the application displayed in nav bar")]然后,你就可以做:
@Html.DisplayNameFor(x => ApplicationSettings.Value.ApplicationName)然而,从技术上讲,这也不是你想要的。更有可能的是,您仍然希望显示名称类似于Application Name,除此之外,这应该是真正的描述性文本。Display属性确实有一个可用于此的Description属性:
[Display(Name = "Application Name", Description = "The name of the application displayed in nav bar")]然而,不幸的是,没有内置的方式来实际显示Description。但是,有一个another SO answer包含一个扩展,您可以为此添加一个扩展。
公共静态类HtmlExtensions {公共静态IHtmlContent DescriptionFor(此IHtmlHelper html,Expression>表达式){ if (html ==空)抛出新ArgumentNullException(nameof(html));if (表达式==空)抛出新ArgumentNullException(表达式);var modelExplorer =IHtmlContent html.ViewData,html.MetadataProvider);如果( {ExpressionHelper.GetExpressionText(expression)}");modelExplorer == null)抛出新InvalidOperationException($“未能获得用于modelExplorer的模型资源管理器,则返回新的InvalidOperationException} 提供:韦特·惠森特
https://stackoverflow.com/questions/48574932
复制相似问题