属性为空时,@ Html.DisplayNameForModel()不起作用

时间:2019-05-21 07:46:02

标签: c# asp.net-core

是否使用日期时间属性创建基础模型? (空)使用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

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用@Html.LabelFor(m => m.NullableProperty)?这也应该呈现DisplayName属性值。

答案 1 :(得分:0)

因此,通过进一步研究,发现问题出在@model DateTime行 给我足够提示的信息是this 模板获取DateTime吗?作为可为空的对象,因此不知道它需要访问其他属性来获取某些元数据。

有两种方法可以解决此问题:通用@model dynamic或强类型@model DateTime? 当我在源代码here

中找到此注释时,这一点得到了确认。

We don't want to search for Nullable<T>, we want to search for T (which should handle both T and Nullable<T>).