如何解决ViewTime上的DateTime属性设置不正确
|| 将下面视图的表单发布到控制器动作MyAction
时,我在StartDate属性中获得一个1/1/0001 12:00:00 AM日期,输入日期的格式应为dd / MM / yyyy 。我在表格上将其设置为14/04/2011,但它在1/1/0001中出现。是否没有正确配置某些文化或设置?
在我的控制器中,文化设置为
Thread.CurrentThread.CurrentCulture = new CultureInfo(\"en-AU\");
动作发布到:
[HttpPost]
public ActionResult MyAction(MyViewModel myViewModel)
{
if (!ModelState.IsValid) return View(\"Index\",myViewModel);
...
查看模型:
public class MyViewModel
{
[DataType(DataType.Date)]
[DisplayName(\"Start Date\")]
[Required]
[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = \"{0:dd/MM/yyyy}\")]
public DateTime StartDate { get; set; }
}
在视图上:
<%=Html.EditorFor(m => m.StartDate,new { style = \"width: 110px;\",@class = \"date\" })%>
解决方法
不好的是,我没有在
MyAction
中设置Thread.CurrentThread.CurrentCulture
,仅在Index
动作中设置了。我将逻辑移到所有控制器都扩展的BaseController
类中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。