C# 在 TextBoxFor() 中显示格式化的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15943797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:33:17  来源:igfitidea点击:

Display a formatted date in a TextBoxFor()

c#asp.net-mvcdate

提问by Traffy

I'm using MVC4 and Entity Framework to develop an intranet web app. I have a list of persons which I can edit. When I access the edit view, in the textbox "Start date", the date is displayed like this : 7/11/2013 00:00:00. What I want to do is to display it in the format yyyy/MM/dd. I tried the String.Format("{0:yyyy/MM/dd}", item.StartDate)but it does not work. I also tried with the annotation [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]but it does not work neither.

我正在使用 MVC4 和实体框架来开发 Intranet Web 应用程序。我有一个可以编辑的人员列表。当我访问编辑视图,在文本框“开始日期”,日期显示是这样的:7/11/2013 00:00:00。我想要做的是以yyyy/MM/dd格式显示它。我试过了,String.Format("{0:yyyy/MM/dd}", item.StartDate)但它不起作用。我也尝试使用注释,[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]但它也不起作用。

In my view I have this :

在我看来,我有这个:

<div class="editor-field">
        @Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker" })
        @Html.ValidationMessageFor(model => model.StartDate)
    </div>

Any idea about how to do?

知道该怎么做吗?

采纳答案by Vladimir

@Html.TextBoxFor(m => m.StartDate, 
    new { @Value = Model.StartDate.ToString("yyyy/MM/dd"), @class="datepicker" })

回答by Maxime

enter image description here

在此处输入图片说明

Your question asks for EditorFor()but the code you provided uses TextboxFor().

您的问题要求提供,EditorFor()但您提供的代码使用TextboxFor().

In your Model(e.g. MyModel.cs), you should have:

在您的模型(例如 MyModel.cs)中,您应该具有:

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

In your View(e.g. Index.cshtml), you simply use it like you wrote it:

在您的视图(例如 Index.cshtml)中,您只需像编写它一样使用它:

@Html.EditorFor(m => m.StartDate, new { htmlAttributes = new { @class = "datepicker" } })

It works as expected.

它按预期工作。

By doing it that way (instead of altering the way it's displayed in your View), you could reuse your model somewhere else and don't have to specify how to display the date in your View. So, if for some reason, you have to change the display format, you would only need to change it once.

通过这样做(而不是改变它在您的视图中的显示方式),您可以在其他地方重用您的模型,而不必指定如何在您的视图中显示日期。因此,如果由于某种原因必须更改显示格式,则只需更改一次。

The solution also works for MVC 5.

该解决方案也适用于 MVC 5。

回答by xyvyx

I'm a noob here (and also with MVC) and this is just an variation of Maxime's using a partial class...

我是这里的菜鸟(还有 MVC),这只是 Maxime 使用部分类的一种变体......

If you've got a DB-first model and don't want to alter it directly for fear of losing your changes, just do this same thing in your own partial class. Just add a new class/file within your Models folder and make sure it uses the same namespace as your generated models.

如果你有一个 DB-first 模型并且不想直接改变它,因为害怕丢失你的更改,只需在你自己的部分类中做同样的事情。只需在您的 Models 文件夹中添加一个新的类/文件,并确保它使用与您生成的模型相同的命名空间。

So here's how you would declare the metadata to both format the date & modify the display name of another value (ie: to abbreviate the column headers in a table)

所以这里是你如何声明元数据来格式化日期和修改另一个值的显示名称(即:缩写表中的列标题)

namespace MyProjectName.Models
{

    public class MyModelMeta
    {

        [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]  // format used by Html.EditorFor
        public DateTime StartDate;

        [Display(Name = "User ID")] // abbreviation shown in Html.DisplayNameFor
        public string SomeReallyLongUserIDColumn;

    }

    [MetadataType(typeof(MyModelMeta))]
    public partial class MyModel
    {

    }
}

回答by s.c

Html.EditorFor also work

Html.EditorFor 也工作



@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "datepicker", @Value = model.StartDate.ToString("yyyy/MM/dd") } })

回答by Mauricio Chario Fuentes Chaves

The top easy for me , was adding the typeattribute,

对我来说最简单的是添加type属性,

@Html.EditorFor(model => model.FechaRegistro, new { htmlAttributes = new { @class = "form-control oso" ,@type = "date"  } })

回答by nAyeem

try this.. convert the date value as a htmlAttribute within textboxfor.. that can solve your problem

试试这个..将日期值转换为 textboxfor.. 中的 htmlAttribute 可以解决您的问题

<div class="editor-field">
    @Html.TextBoxFor(model => model.StartDate, new { htmlAttributes = new { @Value = Model.StartDate.ToString("yyyy/MM/dd"), type = "date"  }})
    @Html.ValidationMessageFor(model => model.StartDate)
</div>