在强类型视图中格式化可为 null 的 DateTime 字段

发布于 2024-09-18 17:41:32 字数 661 浏览 5 评论 0原文

我的模型中有一个具有 BornDate 属性的 Person 类,定义为

[DisplayName("Born Date")]
public DateTime? BornDate { get; set; }

我在视图中使用此字段作为

<td style="white-space:nowrap">
    <%= Html.LabelFor(model => model.BornDate)%>
    <br />
    <%= Html.TextBoxFor(model => model.BornDate, new { id = "bornDate" })%>
    <%= Html.ValidationMessageFor(model => model.BornDate, "*")%>
</td>

问题是,当我使用 BornDate 文本框编辑 Person 实例时,其格式设置为,

dd/MM/yyyy hh.mm.ss

而我想将其格式化而不使用时间部分(“dd/MM/yyyy”)。我无法将 toString 方法与格式字符串一起使用,因为它是一个可为空的字段。

我能做些什么?

I have a Person class with a BornDate property in my model defined as

[DisplayName("Born Date")]
public DateTime? BornDate { get; set; }

I use this field in my view as

<td style="white-space:nowrap">
    <%= Html.LabelFor(model => model.BornDate)%>
    <br />
    <%= Html.TextBoxFor(model => model.BornDate, new { id = "bornDate" })%>
    <%= Html.ValidationMessageFor(model => model.BornDate, "*")%>
</td>

The problem is that when I am editing a Person instance with the BornDate text box is formatted as

dd/MM/yyyy hh.mm.ss

while I would like to format it without the time part ("dd/MM/yyyy"). I am not able to use the toString method with the format string because it is a nullable field.

what can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沩ん囻菔务 2024-09-25 17:41:32

您应该能够使用价值。首先检查它不为空。

var displayDate = model.BornDate.HasValue ? model.BornDate.Value.ToString("yyyy") : "NoDate";

You should be able to use Value. Just check it isn't null first.

var displayDate = model.BornDate.HasValue ? model.BornDate.Value.ToString("yyyy") : "NoDate";
凹づ凸ル 2024-09-25 17:41:32

我知道这已经很晚了,但我正在研究另一个问题并遇到了这个问题。有一种方法可以只显示日期部分,不需要表示层格式化。

[DisplayName("Born Date")]
[DataType(DataType.Date)]
public DateTime? BornDate { get; set; }

使用此功能将确保您在视图上绑定到此属性的任何地方,只有日期会显示

希望这对某人有帮助

I know this is very late but I was researching another problem and came across this issue. There is a way to only show the date part which does not require presentation layer formatting.

[DisplayName("Born Date")]
[DataType(DataType.Date)]
public DateTime? BornDate { get; set; }

Using this will ensure that everywhere you bind to this property on your View, only the date will show

Hope this helps somebody

弄潮 2024-09-25 17:41:32

要在 VIEW 中执行此操作,您也可以使用 Razor 语法:

 @Html.TextBox("BornDate", item.BornDate.HasValue ? item.BornDate.Value.ToShortDateString():"") 

To do this in your VIEW, you can use Razor syntax, as well:

 @Html.TextBox("BornDate", item.BornDate.HasValue ? item.BornDate.Value.ToShortDateString():"") 
耳钉梦 2024-09-25 17:41:32

您可以这样做,它将适用于 Nullable 模型类型:

@model DateTime?
@{
    object txtVal = "";
    if (Model.HasValue) 
    {
        txtVal = Model.Value;
    };
}
@Html.TextBoxFor(x => Model, new {Value = string.Format(ViewData.ModelMetadata.EditFormatString, txtVal))

You can do this, it will work with Nullable model types:

@model DateTime?
@{
    object txtVal = "";
    if (Model.HasValue) 
    {
        txtVal = Model.Value;
    };
}
@Html.TextBoxFor(x => Model, new {Value = string.Format(ViewData.ModelMetadata.EditFormatString, txtVal))
时光礼记 2024-09-25 17:41:32

我解决这个问题的另一种方法是向 TextBoxFor 添加格式

@Html.TextBoxFor(model => model.BornDate, "{0:MM/dd/yyyy}", new { id = "bornDate" })

Another way I've tackled this issue is by adding format to the TextBoxFor

@Html.TextBoxFor(model => model.BornDate, "{0:MM/dd/yyyy}", new { id = "bornDate" })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文