MVC3&安培;剃刀 - 更改日期时间字符串从" MM / DD / YYYY 12:00:00 AM"到" MM / DD / YYY"在TextBox

MVC3&安培;剃刀 - 更改日期时间字符串从" MM / DD / YYYY 12:00:00 AM"到" MM / DD / YYY"在TextBox

问题描述:

我想展示在使用的DateTime生日文本字段上停止时间

I am trying to stop the Time from showing up in a Birthday Text Field that uses DateTime

我的code:(我使用jQuery的DatePicker)

My Code: (I'm using the Jquery DatePicker)

<label for="birthday">Birthday:</label>
            @Html.TextBox("birthday", (Model.Birthday.ToString()), new { @class = "datePicker" })

JavaScript的:

The Javascript:

$(document).ready(function () {
    $('.datePicker').datepicker({ showOn: 'both', buttonImage: "/content/images/calendar-red.gif" });
});

我有模式设置为日期:

I have the Model setup for the date:

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

仍然在文本框中显示文本:

2010年8月21日12:00:00 AM

"8/21/2010 12:00:00 AM"

我要在文本框中的文本到diplay刚才:

2010年8月21日

我曾尝试一切从:

@inherits System.Web.Mvc.WebViewPage<System.DateTime>

但不会让我做,因为我@using模型

but wont let me do that since I am @using a model

我会使用的视图模型编辑器模板和数据批注来指定格式,这使得更清洁的意见:

I would use an editor template and data annotations on the view model to specify formatting which makes the views much cleaner:

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

然后你的视图中:

and then inside your view:

<span class="datePicker">
    @Html.LabelFor(x => x.Birthday)
    @Html.EditorFor(x => x.Birthday)
</span>

和再适应选择,因为文本框将不再有日期选择器类:

and then adapt the selector because the textbox will no longer have the datePicker class:

$('.datePicker :text').datepicker({ 
    showOn: 'both', 
    buttonImage: "/content/images/calendar-red.gif" 
});