asp mvc EditorFor DateTime无法显示

时间:2013-05-30 18:27:41

标签: asp.net-mvc razor

我正在尝试使用以下内容在我的剃刀文件中显示从数据库检索到的日期时间字段:

 @Html.EditorFor(model => model.RequestDate);

我100%确定地知道RequestDate不为null,因为我检查了视图中传递的模型,它是数据库中的日期。但是,日期时间文本框仍显示“mm / dd / yyyy”。我不知道为什么它不显示日期。有任何想法吗?谢谢。

这是我的模特:

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

修改

   [HttpGet]
    public ActionResult DispatchResponseIndex()
    {
        DispatchResponseModel model = new DispatchResponseModel();       


        //if the users session id isnt null then we know they have data related to this form
        object UserID = Session["TestID"];
        if (UserID == null)
        {
            return View(model); //default view               
        }
        else
        {
            UserID = Session["TestID"];
        }

        LoadDB(model, (Guid)UserID);
     //looking at the model here the RequestDate has a valid date of  //{5/24/2013 12:00:00 AM}

        return View(model); 
    }

编辑2:

如果我使用@ Html.TextBox(“asdasd”,Model.RequestDate);然后它将以字符串格式显示保存的日期,但我需要使用文本框的内置日期编辑器。

7 个答案:

答案 0 :(得分:20)

这确实会引起混乱并使我恼火!

如果您使用DataType.Date,则需要将其设置为

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

采用该格式,使用破折号。您可以尝试添加lang标记,但Chrome会检查本地计算机的日历格式,并且该格式将始终取代任何内容。 (如果更改计算机区域设置,则必须完全重新启动Chrome)

<html lang="en-GB">

enter image description here

当Chrome显示日期选择器时,它将位于计算机区域设置的正确区域设置中。这真好?但有些控制会很好!无论如何它对我有用。

这样,当您加载页面时,如果设置了日期,则会填充日期,并且用户可以选择新日期。

enter image description here

答案 1 :(得分:13)

<强> FINAL

我是个白痴。我不知道Chrome有一个添加的日期选择器。我认为这是MVC内置的东西。我需要使用一个实际的jquery日期选择器。感谢大家的所有信息。

答案 2 :(得分:3)

在模型中添加格式化行

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

<强>更新

OR

您可以将模型属性datetime更改为string。并将你的datetime对象转换为字符串!!

OR

或者您可以在编辑器字段中格式化该值。对于样本

@Html.TextBoxFor(model => model.FromDate, "{0:d}")

答案 3 :(得分:2)

实际上直到我看到这篇文章,我才意识到Chrome和IE之间存在“日期选择器”差异,即使我从MVC的教程中获得了示例页面布局。

对于没有意识到差异的人,正如我之前所做的那样,请在下面找到比较:

enter image description here

答案 4 :(得分:0)

我有同样的问题。如果您查看返回Controller的HTTP Post以及POST中日期的格式,则为yyyy-MM-dd。

我将数据注释更改为此并且有效...除了现在,索引列表视图显示yyyy-mm-dd格式。编辑在datepicker控件中显示mm / mm / yyyy?

已添加到型号日期 -

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

恕我直言,自1980年以来我见过的应用程序都使用了日期和时间。为什么这对MS先生来说仍然是一团糟?

答案 5 :(得分:0)

这是我在模型中的方式

[Display(Name = "Effective Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yyyy}")]
    public DateTime? Effectivedate 
    {
        get { return EffectivedateEdit; }
        set { EffectivedateEdit = value; EffectivedateEdit = value; } 
    }


    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime? EffectivedateEdit { get; set; }

答案 6 :(得分:0)

DataFormatString 必须完全像这样“{0:yyyy-MM-dd}”然后才@Html.EditorFor 与日期选择器控件一起使用。