MVC EditorFor日期不显示值 [英] MVC EditorFor Date not showing Value
问题描述
我想在MVC应用程序中从模型中以德语日期格式(dd.MM.yyyy)显示日期,并为用户提供日期选择器.我已经解决了有关该主题的许多问题,但仍然无法解决.我在Firefox和Edge中进行了尝试,每个步骤都得到了相同的结果.
I want to display a date in German date format (dd.MM.yyyy) from the model in an MVC app and provide the user a datepicker. I've gone throug a lot of the questions that have already been answered on this topic, but still can't get it to work. I tried it in Firefox and Edge, with the same results for each step.
第1步:
型号:
private DateTime? inbetriebnahmedatum;
public DateTime? Inbetriebnahmedatum
{
get { return inbetriebnahmedatum; }
set { inbetriebnahmedatum = value; OnPropertyChanged(new PropertyChangedEventArgs("Inbetriebnahmedatum")); }
}
查看:
@Html.EditorFor(model => model.Inbetriebnahmedatum, new { htmlAttributes = new { @class = "form-control", placeholder = "Datum eingeben" } })
结果:无日期选择器,显示时间,验证错误
Result: No Datepicker, Time is displayed, Validation error
第2步:添加了数据类型
型号:
private DateTime? inbetriebnahmedatum;
[DataType(DataType.Date)]
public DateTime? Inbetriebnahmedatum
{
get { return inbetriebnahmedatum; }
set { inbetriebnahmedatum = value; OnPropertyChanged(new PropertyChangedEventArgs("Inbetriebnahmedatum")); }
}
查看:未更改
结果:未加载值.
第3步:添加DisplayFormat,删除数据类型
型号:
private DateTime? inbetriebnahmedatum;
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime? Inbetriebnahmedatum
{
get { return inbetriebnahmedatum; }
set { inbetriebnahmedatum = value; OnPropertyChanged(new PropertyChangedEventArgs("Inbetriebnahmedatum")); }
}
查看:未更改
结果:显示日期,但没有日期选择器和验证错误
Result: Date displayed, but no datepicker and Validation Error
第4步:将ui文化添加到Web.config
Web.config
Web.config
<globalization uiCulture="de-DE" culture="de-DE" />
结果:相同
第5步:再次添加数据类型
型号:
private DateTime? inbetriebnahmedatum;
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime? Inbetriebnahmedatum
{
get { return inbetriebnahmedatum; }
set { inbetriebnahmedatum = value; OnPropertyChanged(new PropertyChangedEventArgs("Inbetriebnahmedatum")); }
}
结果:未加载值,显示日期选择器
Result: Value not loaded, datepicker shows
推荐答案
我不确定,但是您可以尝试 尝试输入类似>>的字符: 2018年1月1日
i'm not sure but u can try this try enter as like >> : 01/01/2018
或
[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}")]
或
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
这篇关于MVC EditorFor日期不显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!