【问题标题】:Pass DateTime variable to Edit.cstml to display and select from datepicker将 DateTime 变量传递给 Edit.cstml 以显示并从 datepicker 中选择
【发布时间】:2018-01-07 08:03:01
【问题描述】:

我有以下视图 Edit.cshtml 片段

<div class="form-group">
                @Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
                </div>
            </div>

属性 BirthDate 是 DateTime 的类型。 当我想编辑 BirthDate 时,它​​只显示格式 rrrr-mm-dd 没有数据,但可以从 datepicker 中选择日期。当我添加

@Html.EditorFor(model => model.BirthDate.Date, new { htmlAttributes = new { @class = "form-control" } })

然后我收到格式为 yyyy-mm-dd HH:MM:SS 的数据,但我无法从 datepicker 中选择日期。

我应该怎么做,以 yyyy-mm-dd 格式获取日期并有可能从 datepicker 中选择日期?

【问题讨论】:

  • 尝试在您的 BirthDate 属性中使用数据注释。

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


【解决方案1】:

在您的Model 中添加以下模型属性。

    [DataType(DataType.Date)]
    public DateTime BirthDate{ get; set; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多