C# 在 MVC5 中使用 Html.DisplayFor() 的日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19920603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:07:59  来源:igfitidea点击:

Date Format using Html.DisplayFor() in MVC5

c#asp.net-mvcdatetimeformattingconditional

提问by Greg Hayden

Referencing the answer in this postI added /Views/Shared/DisplayTemplates and added a partial view called ShortDateTime.cshtml as shown below:

参考这篇文章中的答案,我添加了 /Views/Shared/DisplayTemplates 并添加了一个名为 ShortDateTime.cshtml 的部分视图,如下所示:

@model System.DateTime
@Model.ToShortDateString()

When the model contains a value this works and the formatted date is displayed correctly:

当模型包含一个值时,这有效并且格式化的日期显示正确:

@Html.DisplayFor(modelItem => item.BirthDate, "ShortDateTime")

However, if a null value is returned a 'System.InvalidOperationException' is thrown. Indicating:

但是,如果返回空值,则会抛出“System.InvalidOperationException”。表示:

{"The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'."}

{"传入字典的模型项为空,但该字典需要一个非空的'System.DateTime'类型的模型项。"}

My first inclination was to use an if statement inside the partial view but it didn't seem to matter. Without referencing the template null values are handled as in:

我的第一个倾向是在局部视图中使用 if 语句,但这似乎无关紧要。不引用模板空值的处理方式如下:

@Html.DisplayFor(modelItem => item.BirthDate)

but the original issue of formatting remains. When I try to put conditional formatting in the View as follows, it doesn't work but I hoping it's just a syntax thing.

但最初的格式化问题仍然存在。当我尝试按如下方式在视图中放置条件格式时,它不起作用,但我希望它只是一个语法问题。

@Html.DisplayFor(modelItem => item.BirthDate == null) ? string.Empty : (modelItem => item.BirthDate, "ShortDateTime"))

The above results in a different 'System.InvalidOperationException':

以上导致不同的“System.InvalidOperationException”:

{"Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."}

{"模板只能用于字段访问、属性访问、单维数组索引或单参数自定义索引器表达式。"}

So, is there a way to do conditional formatting in the View to generate just the date from a DateTime value?

那么,有没有办法在视图中进行条件格式设置以仅从 DateTime 值生成日期?

回答by wdosanjos

Have you tried this?

你试过这个吗?

@if (modelItem.BirthDate != null) { Html.DisplayFor(modelItem => item.BirthDate, "ShortDateTime") }

回答by abc123

I guess you should declare the BirthDate property as nullable in model class

我想您应该在模型类中将 BirthDate 属性声明为可为空

public DateTime? BirthDate{ get; set; }

and you must have declared as

你必须声明为

public DateTime BirthDate{ get; set; }

this will expect a value every time.

这每次都会期望一个值。

if you set as nullable it will not expect a value.

如果您设置为可空,则不会期望值。

回答by Vinney Kelly

The problem you're experiencing is that you are passing a nullvalue into a non-nullable model. Change the partial view's model to DateTime?. For example:

您遇到的问题是您将null值传递到不可为空的模型中。将局部视图的模型更改为DateTime?. 例如:

@model DateTime?          
@if (!Model.HasValue)
    {
    <text></text>
}
else
{
    @Model.Value.ToShortDateString()
}

Hope this helps.

希望这可以帮助。

回答by Greg Hayden

By applying the conditional statement ahead of the Html helper, only non null values get passed.

通过在 Html 帮助器之前应用条件语句,只会传递非空值。

@if (item.BirthDate != null) { @Html.DisplayFor(modelItem => item.BirthDate , "ShortDateTime")}