c# - 通过 DateTime 对象仅从 Controller 传递日期。尝试多种方式绑定(bind)时间

标签 c# asp.net-mvc date datetime model-view-controller

我的 .cs 文件包含这个。

enter image description here

模型还包含相同的数据类型

在表格中填写数据时,我得到的值是

enter image description here

在 SQL 中,我将数据类型保留为 date,在模型中,有必要保留 DateTime。

我尝试了 ParseExact().Date 等,但最终一切都随着时间的推移而出现。

enter image description here

有人可以帮忙吗? 我尝试了在此站点中找到的与此问题相关的许多解决方案,但没有得到任何帮助。

提前致谢。

最佳答案

将此添加到您的模型

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

在 .cshtml 中

@Html.TextBoxFor(m => m.LastPaymentDate, "{0:dd/MM/yyyy}")

关于c# - 通过 DateTime 对象仅从 Controller 传递日期。尝试多种方式绑定(bind)时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48002507/

相关文章:

c# - 无法将类型 'System.Collections.Generic.List<MODEL#1>' 隐式转换为 'System.Collections.Generic.List<Model#2>

c# - 如何在应用程序配置文件中设置图像文件路径?

c# - 从 usercontrol viewmodel 到 customUsercontrol Viewmodel 的 Unity 依赖注入(inject)

c# - 如何在 XAML 中显示我的枚举?

javascript - 全日历选择错误的结束日期

javascript - 在 Javascript 中格式化来自 Restful Service 的日期

powershell - 如何在Powershell中使用Get-Date的.AddMonths方法

c# - 迭代文件结构时更新进度条

javascript - 大量的页 `view source` 行有意义吗?

c# - 用枚举的名称填充下拉列表