gpt4 book ai didi

c# - 在 html 文本框 mvc 中使用短日期字符串

转载 作者:行者123 更新时间:2023-11-30 18:57:10 27 4
gpt4 key购买 nike

我有一个模型如下,我正在使用 Linq to SQL

public class CustomerDetails
{
public Customer customer { get; set; }

}

“客户”由数据库中的单个客户填充。客户有一个字段“NextEmailDate”。我想将它绑定(bind)到一个文本框,但只显示日期而不是时间

@Html.TextBoxFor(m=> m.Customer.NextEmailDate)

但是我得到一个文本框,其中包含文本 16/09/2012 00:00:00 我该如何将其设置为 16/09/2012?

最佳答案

假设 Customer 是您的实际实体,引入一个 View 模型并将其传递给您的 View 。这样你就可以用你需要的所有格式/验证属性来装饰它,例如

public class CustomerViewModel
{
[Required(ErrorMessage = "Next email date is required!")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}"]
public DateTime NextEmailDate { get; set; }
...
}

public class CustomerDetails
{
public CustomerViewModel Customer { get; set; }
}

您可以手动映射属性或使用类似 AutoMapper 的库.

关于c# - 在 html 文本框 mvc 中使用短日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12623895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com