我正在使用ASP.NET MVC 5
。我的创建和编辑表单中只有一个日期选择器。在创建形式,它的工作正常。但在编辑形式,它加载以前保存的DateTime
作为dd-MM-yyyy hh:mm:ss
。单击文本框时,我的日期选择器显示日期为1899年12月。只有我可以格式化我的DateTime
,它才能正常工作。
虽然,我知道如何在Struts 2中解决这个问题,但我是ASP.NET MVC的新手。任何人都可以告诉我如何在我的视图中将我的DateTime
模型属性格式化为Date?
这是我的模特: -
public class Vehicle
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VehicleId
{ get; set; }
[Display(Name = "Vehicle name")]
[StringLength(100)]
public String Name
{ get; set; }
[Required]
public String VehicleType
{ get; set; }
[Required]
[StringLength(20)]
[Display(Name = "Registration no.")]
public String RegNo
{ get; set; }
[DataType(DataType.Date)]
[Display(Name = "Insurance date")]
public DateTime InsuranceDate
{ get; set; }
}
以下是我观点的一部分: -
<div class="form-group">
@Html.LabelFor(model => model.InsuranceDate, new { @class = "control-label col-md-2" })
<div class="col-md-6 input-group date" id="datePick">
@Html.EditorFor(model => model.InsuranceDate, new { @class = "form-control", id = "datePickText", placeholder = "Enter Insurrance Date" })<span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
@Html.ValidationMessageFor(model => model.InsuranceDate)
</div>
</div>
public class AsOfdates
{
public string DisplayDate { get; set; }
private DateTime TheDate;
public DateTime DateValue { get { return TheDate.Date; } set { TheDate = value; } }
}
您需要在您的属性中建立格式。您可以在此处查看更多信息: