ASP.NET DisplayFormat dd/MM/yyyy无法正常工作

4

我的模型

[Display(Name = "Tanggal Lahir")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime TanggalLahir { get; set; }

我的观点
@Html.EditorFor(m => m.TanggalLahir)

但它仍然以MM/dd/yyyy格式显示数据。 即使我的web.config文件中
<system.web>
  <globalization uiCulture="en" culture="en-GB"/>
  <authentication mode="None" />
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" maxRequestLength="30000000" />
</system.web>
4个回答

1
声明您的属性为字符串。
[Display(Name = "Tanggal Lahir")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public string TanggalLahir { get; set; }

0
@Html.EditorFor(m => m.TanggalLahir, "{0:dd/MM/yyyy}", new {maxlength = 10})

你可以尝试这样做...


0
如果您在模型上无法使其工作,可以尝试在视图上进行。
 @Html.EditorFor(m => m.TanggalLahir, "{0:dd/MM/yyyy}", new {maxlength = 10})

(或者)

  @Html.EditorFor(m => m.TanggalLahir, new { @Value = Model.StartDate.ToString("dd/MM/yyyy") })

如果我将其移除,输入框就不会显示日期选择器下拉菜单 :( - Onchomngebul
这里是正确的格式 [DisplayFormat(DataFormatString = "{0:dd'/'MM'/'yyyy}", ApplyFormatInEditMode = true)]。 - Manraj
抱歉,还是不行 >_< 我觉得问题可能出在其他地方 :/ - Onchomngebul
你能否将它改为DataFormatString = "{0:d}"? - Manraj
就像这样[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] - Manraj

0

请查看this,那里有一些有用的信息可能会对您有所帮助。同时,请确保将CultureInfo指定为不变文化。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接