繁体   English   中英

我无法在 ASP.NET MVC 5. 应用程序的视图中转换我的日期时间格式。 我该如何解决?

[英]I can't convert my datetime format in the View of a ASP.NET MVC 5. Application. How do I solve it?

我正在尝试更改 @Html.EditorFor 的日期时间格式,但我不能。 我正在尝试通过数据注释来做到这一点。

那是我的模型的代码。

 //------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Dominio_FlowerMorena.Entidades { using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; public partial class Tabela_Participante { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Tabela_Participante() { this.Tabela_Participante1 = new HashSet<Tabela_Participante>(); } public long ID_Participante { get; set; } public string Nome { get; set; } public string CPF { get; set; } public string RG { get; set; } [Display(Name = "Data de Retorno")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")] [DataType(DataType.Date, ErrorMessage = "Data em formato inválido")] public System.DateTime Data_Nascimento { get; set; } public string Sexo { get; set; } public string Estado_Civil { get; set; } public int Numero_Dependentes { get; set; } public long FK_Endereco { get; set; } public string E_mail { get; set; } public string Telefone { get; set; } public string Celular { get; set; } public string Operadora { get; set; } public long FK_Conta { get; set; } public string Segundo_Titular { get; set; } public Nullable<long> FK_Patrocinador { get; set; } public virtual Tabela_Conta Tabela_Conta { get; set; } public virtual Tabela_Endereco Tabela_Endereco { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Tabela_Participante> Tabela_Participante1 { get; set; } public virtual Tabela_Participante Tabela_Participante2 { get; set; } public virtual Tabela_Usuario Tabela_Usuario { get; set; } } }

Ant 这是我的视图代码:

 @model FlowerMorena_WebUI.Models.ViewModelFicha @{ ViewBag.Title = "CadastrarFicha"; } <h2>CadastrarFicha</h2> <!DOCTYPE html> <html> <head> <link href="~/Content/bootstrap.css" rel="stylesheet" /> <link href="~/Content/bootstrap-theme.css" rel="stylesheet" /> <meta name="viewport" content="width=device-width" /> <title>RsvpForm</title> <link href="~/Content/Style.css" rel="stylesheet" /> </head> <body> <div class="panel panel-success"> <div class="panel-heading text-center"><h4>Participante</h4></div> <div class="panel-body"> @using (Html.BeginForm()) { @Html.ValidationSummary(); <div class="form-group"> <label> Nome do participante: </label> @Html.TextBoxFor(x => x.Participante.Nome, new { @class = "form-control" }) </div> <div class="form-group"> <label> Segunda titular: </label> @Html.TextBoxFor(x => x.Participante.Segundo_Titular, new { @class = "form-control" }) </div> <div class="form-group"> <label> CPF: </label> @Html.TextBoxFor(x => x.Participante.CPF, new { @class = "form-control" }) </div> <div class="form-group"> <label> RG: </label> @Html.TextBoxFor(x => x.Participante.RG, new { @class = "form-control" }) </div> <div class="form-group"> <label> Sexo: </label> @Html.DropDownListFor(x => x.Participante.Sexo, new[] { new SelectListItem() {Text = "Masculino", Value = "M"}, new SelectListItem() {Text = "Feminino", Value = "F" } }, "Sexo", new { @class = "form-control" }) </div> <div class="form-group"> <label> Operadora: </label> @Html.TextBoxFor(x => x.Participante.Operadora, new { @class = "form-control" }) </div> <div class="form-group"> <label> Celular: </label> @Html.TextBoxFor(x => x.Participante.Celular, new { @class = "form-control" }) </div> <div class="form-group"> <label> Telefone: </label> @Html.TextBoxFor(x => x.Participante.Telefone, new { @class = "form-control" }) </div><br/> <div class="form-group"> <label> Estado Civil: </label> @Html.TextBoxFor(x => x.Participante.Estado_Civil, new { @class = "form-control" }) </div> <div> <label> E_Mail: </label> @Html.TextBoxFor(x => x.Participante.E_mail, new { @class = "form-control" }) </div> <div> <label> Número de dependentes: </label> @Html.TextBoxFor(x => x.Participante.Numero_Dependentes, new { @class = "form-control" }) </div> <div> <label> Data de Nascimento: </label> <br /> @Html.EditorFor(x => x.Participante.Data_Nascimento, new { htmlAttributes = new { @class = "form-control" } }) <br/> </div> <div class="text-center"> <input class="btn btn-success" type="submit" value="Cadastrar Paricipante" /> </div> } </div> </div> </body> </html>

我在 Data_Nascimento 字段中使用数据注释,但该视图未应用正确的格式。 我想要 'yyyy-MM-dd' 并且 View 返回给我 'MM-dd-yyyy'。 我该如何解决? 谢谢。

您指的是何时实际输出现有值(即在 POST 发生后或显示现有数据时)或何时实际设置该值?

您的实际[DisplayFormat]属性显示正确:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]

如果你想以另一种格式显示内容,你可以调整DataFormatString如下所示:

// Display as "yyyy/MM/dd"
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]

因此,如果您的Data_Nascimento值没有预定义的值,则应呈现初始值为0001-01-01的 TextBox :

在此处输入图片说明

如果您只是提交此表单,则您的模型应填充与此对应的 DateTime,如果您执行ToString()调用,它将如下所示:

在此处输入图片说明

因此,如此处的交互式示例中所示,正确发布了适当的值。 因此,在您提供的当前视图中,没有什么是“错误”的,但重要的是要记住 DateTime 对象不知道任何格式规则,因此如果您使用的是 Visual Studio 调试器,请参阅“2016 年 4 月 4 日” ",这并不意味着您的 DateTime 对象的格式是这样的

暂无
暂无

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

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