![](/img/trans.png)
[英]How can I automatically mark required field labels with a * (based on [Required] annotation) in a razor view using asp.net core 3.1/5 mvc?
[英]ASP.NET Core 3.1 - Razor View Blank Date Field
在 ASP.NET Core 3.1 项目中,我在使用 model 的 Razor 视图中的日期显示有一个奇怪的问题。
Model
[Display(Name = "Date")]
[DataType(DataType.Date)]
[Required(ErrorMessage = "Date is required")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime ExpenseDate { get; set; }
日期设置在 Controller
ne.ExpenseDate = DateTime.Today;
Razor 查看/表格
@Html.EditorFor(m => m.ExpenseDate, new { htmlAttributes = new { @id = "expenseDate", @class = "form-control", @style = "width: 160px; padding: 4px;" } })
以上在HTML中渲染如下:
<input class="form-control text-box single-line" data-val="true" data-val-required="Date is required" id="expenseDate" name="ExpenseDate" style="width: 160px; padding: 4px;" type="date" value="2021-05-19" />
问题
在桌面/Chrome(调试)上,文本框正确显示:“19-May-2021”
在桌面/Chrome(实时站点)上,文本框显示:“dd-----yyyy”
在 iPhone 和三星平板电脑上,什么都没有显示 - 文本框是空的。
我做了以下更改,似乎已经解决了这个问题,虽然我不太确定为什么它现在可以工作或为什么它以前不能工作:
将 model 中的 DateFormat 字符串更改为:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
将在 model 中设置日期的代码更改为:
ExpenseDate = DateTime.Now.Date;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.