控制器代码-
ViewData["fromDate"] = formcollection.fromDate;(毫无疑问,我可以在javascript中看到viewdata值)
。cshtml视图,& lt;输入类型="datetime-local"id ="fromDate"值="@ViewData["fromDate"/祝辞
上面的赋值不能工作。
<input type="datetime-local" id="fromDate" value="@(((DateTime)ViewData["fromDate"]).ToString("yyyy-MM-dd"))" />
但是,如果您使用的是ASP。. NET Core中,你应该使用ViewModel和输入标签帮助器。然后在标签帮助器上设置格式字符串,它负责正确格式化日期:
public class MyViewModel
{
// various properties for the view
public DateTime FromDate {get;set;}
}
然后你的标签帮助器:
<input asp-for="FromDate" asp-format="yyyy-MM-dd" />
注意根据RFC 3339格式必须是yyyy-MM-dd
。datetime-local控件将负责在用户的区域设置中显示值。
我在这里写过关于在ASP中使用DateTimes的博客。净的核心。剃须刀页面集中,但原则也适用于MVC: https://www.mikesdotnetting.com/article/352/working-with-dates-and-times-in-razor-pages-forms