【问题标题】:Why is my date control empty when page loads?为什么页面加载时我的日期控件为空?
【发布时间】:2015-09-25 09:11:58
【问题描述】:

页面加载时,我的日期控件始终为空。 这是为什么? 该控件的数据库中有数据。

<div class="form-group">
    @Html.LabelFor(model => model.StartDate, new { @class = "control-    label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.StartDate, new { type = "Date" })
        @Html.ValidationMessageFor(model => model.StartDate)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.EndDate, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.EndDate, new { type = "Date" })
        @Html.ValidationMessageFor(model => model.EndDate)
    </div>
</div>

【问题讨论】:

  • 您需要添加更多细节 - 控制器的代码和示例模型数据。
  • 您是否在控制器中为要传递给视图的模型填充 StartDate?
  • 您将输入指定为 type="date",这会呈现 HTML5 日期选择器(注意它仅在 Chrome 中支持),这意味着日期必须采用 ISO 格式 (yyyy-MM-dd)。将[DisplayFormat] 属性添加到您的属性
  • @StephenMuecke:请不要在 cmets 中提供答案。你对这个问题以及如何解决它是绝对正确的,但现在有人需要扯掉你的评论,否则这将无限期地得不到答复。

标签: asp.net asp.net-mvc datetime datepicker


【解决方案1】:

正如@Coulton 所说,在渲染页面之前,请确保您确实在控制器上填充了模型的StartDateEndDate 属性。此外,如果您在课堂上使用 [DataType(DataType.Date)] 标志,您可以丢弃 TextBoxFor 助手,而使用 EditorFor 助手。

在创建或编辑视图之前填充数据属性的示例(这似乎是您尝试在那里构建的视图):

Controller.cs,ganho.Data 是带有 [DataType(DataType.Date)] 标志的 DateTime 属性

[HttpGet]
    public ActionResult Edit(int? id)
    {
        // Omitted code
        var result = from ganho in db.Ganhos
                     where ganho.GanhoID == id.Value
                     where ganho.Usuario.Id == user.Id
                     select new GanhoEditViewModel
                     {
                         GanhoID = ganho.GanhoID,
                         Valor = ganho.Valor,
                         Comentario = ganho.Comentario,
                         Data = ganho.Data,
                         Descricao = ganho.Descricao,
                         Tipo = ganho.Tipo
                     };
        if (result.Count() < 1)
        {
            // 404, no such item exists
            return HttpNotFound();
        }
        return View(result.Single());
    }

编辑.chtml

<!-- Hidden Code, not relevant to the question -->
<div class="form-group">
    @Html.LabelFor(model => model.Data, htmlAttributes: new { @class = "control-label col-md-2"})
    <div class="col-md-10">
        @Html.EditorFor(model => model.Data, new { htmlAttributes = new { @class = "form-control", @id = "dataInput"} })
        @Html.ValidationMessageFor(model => model.Data, "", new { @class = "text-danger"})
    </div>
</div>

如果您正在设置创建视图,您可以像这样初始化字段:

Controller.cs,ganho.Data 是我的 DateTime 属性,用户将显示占位符数据。

[HttpGet]
public ActionResult Create()
{
    var model = new Ganho()
    {
        // ...
        Data = DateTime.Now,    // Using the current date and time as a placeholder
        // ...
    };
    return View(model);
}

请注意,在所有示例中,我都使用 Strongy Typed 视图!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多