使用jQuery datepicker从ASP MVC @ Html.TextBoxFor Control中删除时间

Nea*_*alR 7 asp.net-mvc jquery datepicker asp.net-mvc-3

如何在使用jQuery时阻止显示时间.datepicker()?以下是两个属性的模型部分(采用日期和到期日期)

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    [DisplayName("Expiration Date")]
    public string ExpirationDate { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    [DisplayName("Date Taken")]
    public string DateTaken { get; set; }
Run Code Online (Sandbox Code Playgroud)

使用@Html.EditorFor我正在处理的视图上的控件可以正常工作.但是,当我从中更改该控件时:

 @Html.EditorFor(model => model.DateTaken)
Run Code Online (Sandbox Code Playgroud)

对此:

@Html.TextBoxFor(m => m.DateTaken, new { id = "dtkn" })
Run Code Online (Sandbox Code Playgroud)

为了使这个工作(标签哪个控件已datepicker应用于它):

<script>
    $(function () {
        $("#dtkn").datepicker('setDate');
        $("#exp").datepicker('setDate');
    });

    $.datepicker.setDefaults({
        buttonImageOnly: true,
        buttonImage: '<%=Url.Content("~/Content/images/magnify.gif") %>',
        defaultDate: -1,
        gotoCurrent: true,
        prevText: "<< Prev",
        nextText: "Next >>"
    });
</script>
Run Code Online (Sandbox Code Playgroud)

突然间,时间开始出现了.有什么建议?

编辑

我尝试过以下格式,但没有一种可以使用:

$("#dtkn").datepicker({ dateFormat: 'dd-mm-yy' }).val();
$("#dtkn").datepicker({ dateFormat: 'yy-mm-dd' });
$("#dtkn").datepicker('getDate').format('yyyyMMdd');
Run Code Online (Sandbox Code Playgroud)

还有这个:

$("#dtkn").datepicker('getDate').datepicker('dd/mm/yy');
$('#dtkn').datepicker('option', { dateFormat: 'd MM y' });
Run Code Online (Sandbox Code Playgroud)

以下是我看到Chrome调试控制台的错误:

Uncaught TypeError: Cannot call method 'datepicker' of null 
Run Code Online (Sandbox Code Playgroud)

Jas*_*sen 15

编辑:MVC4用户可以使用此功能

你想要这个超载

@Html.TextBoxFor(m => m.DateTaken, "{0:MM/dd/yyyy}", new { id="dtkn" })
Run Code Online (Sandbox Code Playgroud)

如果你愿意手动放弃助手

<input type="text" value="@Model.DateTaken.ToString("MM/dd/yyyy")" id="dtkn" />
Run Code Online (Sandbox Code Playgroud)


Nea*_*alR 4

通过使用@Html.TextBox而不是@Html.TextBoxFor我能够找到有效的重载。

@Html.TextBox("DateTaken", Model.DateTaken.ToString("MM/dd/yyyy"), new { id = "dtkn" })
Run Code Online (Sandbox Code Playgroud)