简体   繁体   English

文本框的Asp.net MVC Web应用程序日期格式

[英]Asp.net MVC web Application Date Format for Text Box

I am developing a asp.net mvc web app. 我正在开发一个asp.net mvc Web应用程序。

I have an issue with my partial view. 我的部分观点有问题。

Problem is whenever i open my partial view for a "new form" i got a date in text box in following format : 问题是每当我打开“新表单”的局部视图时,我都会在文本框中以以下格式获取日期:

在此处输入图片说明

But when i "edit record" , i get date in my partial view text box like following : 但是,当我“编辑记录”时 ,我会在部分视图文本框中获得日期,如下所示:

在此处输入图片说明

Following is my Model: 以下是我的模特:

[DataType(DataType.Date), DisplayFormat(DataFormatString="{0:d}",ApplyFormatInEditMode=true)]
    public Nullable<System.DateTime> ReleaseDate { get; set; }

Following is my partial view code: 以下是我的部分视图代码:

<div class="editor-field">
        @Html.TextBoxFor(model => model.ReleaseDate, new { @class = "Date" })
        @Html.ValidationMessageFor(model => model.ReleaseDate)
    </div>
    <script type="text/javascript">
        debugger;
        var s = $(".Date");
        s.dateFormat = "dd/mm/yyyy";
        $('.Date').datepicker({ dateFormat: "d/m/yy" });
        var newdate = Date.parse($("#ReleaseDate"));
        var rdate = $("#ReleaseDate").val();
        var fdate = Date.parse(rdate);
        debugger;
    </script>

What i want is to get the date like 16/08/2014 when i edit my record. 我想要的是在编辑记录时得到像2014年8月16日这样的日期。 please tell me where should i modify my code to set date format. 请告诉我我应该在哪里修改代码以设置日期格式。

Thank you 谢谢

You could decorate your view model property with the [DisplayFormat] attribute: 您可以使用[DisplayFormat]属性装饰视图模型属性:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }

and in your view: 并且在您看来:

@Html.EditorFor(x => x.ReleaseDate)

Another possibility which I don't recommend is to use a weakly typed helper: 我不推荐的另一种可能性是使用弱类型的帮助器:

@Html.TextBox("ReleaseDate", Model.ReleaseDate.ToLongDateString())

Below is changes that you should make 以下是您应该进行的更改

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime ReleaseDate { get; [DisplayFormat(DataFormatString =“ {0:dd / MM / yyyy}”,ApplyFormatInEditMode = true)]公共DateTime ReleaseDate {get; set; 组; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM