是否使用日期时间属性创建基础模型? (空)使用DisplayNameForModel或LabelForModel将呈现空白
使用DotNet Core 2.2示例应用程序
Index.cshtml
@model IndexPage
@{
ViewData["Title"] = "Index";
}
<style>
li > div, label { border: solid 1px black; padding: 0.1em; margin: 0.25em; }
label { border-color: grey; }
li > div:first-child:before { content: 'LabelFor: '; }
li > div:nth-child(2):before { content: 'NameFor: '; }
li > div:nth-child(3):before { content: 'DisplayFor: '; }
li.editor > div:nth-child(3):before { content: 'EditorFor: '; }
</style>
<h1>Index</h1>
<ol>
<li>
<div>@Html.DisplayNameFor(m => m.DateTimeNonNullable)</div>
<div>@Html.NameFor(m => m.DateTimeNonNullable)</div>
<div>@Html.DisplayFor(m => m.DateTimeNonNullable)</div>
</li>
<li>
<div>@Html.DisplayNameFor(m => m.DateTimeNullable)</div>
<div>@Html.NameFor(m => m.DateTimeNullable)</div>
<div>@Html.DisplayFor(m => m.DateTimeNullable)</div>
</li>
<li>
<div>@Html.DisplayNameFor(m => m.DateTimeNullableWithDisplay)</div>
<div>@Html.NameFor(m => m.DateTimeNullableWithDisplay)</div>
<div>@Html.DisplayFor(m => m.DateTimeNullableWithDisplay)</div>
</li>
<li>
<div>@Html.DisplayNameFor(m => m.DateTimeNullableWithDisplayName)</div>
<div>@Html.NameFor(m => m.DateTimeNullableWithDisplayName)</div>
<div>@Html.DisplayFor(m => m.DateTimeNullableWithDisplayName)</div>
</li>
<li class="editor">
<div>@Html.DisplayNameFor(m => m.DateTimeNonNullable)</div>
<div>@Html.NameFor(m => m.DateTimeNonNullable)</div>
<div>@Html.EditorFor(m => m.DateTimeNonNullable)</div>
</li>
<li class="editor">
<div>@Html.DisplayNameFor(m => m.DateTimeNullable)</div>
<div>@Html.NameFor(m => m.DateTimeNullable)</div>
<div>@Html.EditorFor(m => m.DateTimeNullable)</div>
</li>
<li class="editor">
<div>@Html.DisplayNameFor(m => m.DateTimeNullableWithDisplay)</div>
<div>@Html.NameFor(m => m.DateTimeNullableWithDisplay)</div>
<div>@Html.EditorFor(m => m.DateTimeNullableWithDisplay)</div>
</li>
<li class="editor">
<div>@Html.DisplayNameFor(m => m.DateTimeNullableWithDisplayName)</div>
<div>@Html.NameFor(m => m.DateTimeNullableWithDisplayName)</div>
<div>@Html.EditorFor(m => m.DateTimeNullableWithDisplayName)</div>
</li>
</ol>
IndexPage.cs
public class IndexPage
{
public DateTime DateTimeNonNullable { get; set; } = DateTime.Today;
public DateTime? DateTimeNullable { get; set; } = DateTime.Today;
[Display(Name = "DateTimeNullableWithDisplay")]
public DateTime? DateTimeNullableWithDisplay { get; set; } = DateTime.Today;
[DisplayName("DateTimeNullableWithDisplayName")]
public DateTime? DateTimeNullableWithDisplayName { get; set; } = DateTime.Today;
}
DisplayTemplates \ DateTime.cshtml
@model DateTime
@Html.LabelForModel()
<label>@Html.DisplayNameFor(m => m)</label>
<span>@Html.ValueForModel()</span>
EditorTemplates \ DateTime.cshtml
@model DateTime
@Html.LabelForModel()
<label>@Html.DisplayNameFor(m => m)</label>
<input type="datetime" id="@Html.IdForModel()" name="@Html.NameForModel()" value="@Html.ValueForModel()"/>
Display Name不会通过LabelForModel或DisplayNameForModel呈现在Nullable条目上(但是DisplayNameFor(m => m.Field)和LabelFor(m => m.Field)可以,但DisplayNameFor(m => m)和LabelFor(m => m)不) Result
答案 0 :(得分:0)
您是否尝试过使用@Html.LabelFor(m => m.NullableProperty)
?这也应该呈现DisplayName
属性值。
答案 1 :(得分:0)