MVC3&安培;剃刀 - 更改日期时间字符串从" MM / DD / YYYY 12:00:00 AM"到" MM / DD / YYY"在TextBox [英] MVC3 & Razor - Change DateTime String from "mm/dd/yyyy 12:00:00 AM" to "mm/dd/yyy" in a Textbox

查看:143
本文介绍了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" 
});

这篇关于MVC3&安培;剃刀 - 更改日期时间字符串从&QUOT; MM / DD / YYYY 12:00:00 AM&QUOT;到&QUOT; MM / DD / YYY&QUOT;在TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆