简体   繁体   English

asp.net mvc datetime未显示其值

[英]asp.net mvc datetime didn't display their value

I have an asp.net mvc 4 project and in my view model have a datetime control. 我有一个asp.net mvc 4项目,并且在我的视图模型中有一个datetime控件。 And into my controller income datetime property format: 09/20/2013 12:00:00 AM and when I try to send it into my view via ViewBag.DateTime and try to display it in my View it didn't display my value in calendar format, just display mm/dd/yyyy and I need to select value from calendar. 并转换为我的控制器收入datetime属性格式:09/20/2013 12:00:00 AM,当我尝试通过ViewBag.DateTime将其发送到我的视图中并尝试在其视图中显示它时,它没有显示我的值日历格式,只显示mm / dd / yyyy,我需要从日历中选择值。 Here is my code: 这是我的代码:

Model 模型

[Display(Name = "Date")]
[DataType(DataType.Date)]
public DateTime DateValue { get; set; }

Controller 控制者

public ActionResult Date(DateTime? DateValue)
{
    if (ExamsDate != null)//ExamsDate have a value and it's sending into view
    {
       ViewBag.DateValue = DateValue;
    }
    return View();
}

View 视图

@Html.LabelFor(model => model.DateValue):<br/>
@Html.EditorFor(model => model.DateValue) //Here instead of 09/20/2013 display mm/dd/yyyy
@Html.ValidationMessageFor(model => model.DateValue)

Anybody know where is my mistake? 有人知道我的错误在哪里吗?

Add this attribute to your model: 将此属性添加到模型中:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]


However if you want an actual calendar control, you'll need to use something like jquery. 但是,如果您想要一个实际的日历控件,则需要使用诸如jquery之类的东西。

我认为您缺少基本的东西,您需要像这样访问可为空的字段值

Datevalue.value instead of just Datevalue and use date time formatting

尝试将您的EditorFor更改为TextBoxFor

@Html.TextBoxFor(model => model.DateValue, new { @Value = Model.DateValue.ToString("dd/MM/yyyy")})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM