MVC3&Razor – 将DateTime字符串从“mm / dd / yyyy 12:00:00 AM”更改为“mm / dd / yyy”在文本框中

我试图阻止时间在使用DateTime的生日文本字段中显示

我的代码:(我正在使用Jquery DatePicker)

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

Javascript:

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

我有模型设置的日期:

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

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

“8/21/2010 12:00:00 AM”

我想要文本框中的文本只显示为:

“2010年8月21日”

我试过一切从:

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

但不要让我这样做,因为我是一个模型

解决方法

我将在视图模型上使用编辑器模板和数据注释来指定格式,使视图更清晰:
[DataType(DataType.Date)]
[displayName("Birthday:")]
[displayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode = true)]
public DateTime? Birthday { get; set; }

然后在你的看法:

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

然后调整选择器,因为文本框将不再具有datePicker类:

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....