none
How to format DateTime in MVC 5 ? RRS feed

  • Frage

  • Hello,

    I can't get formatted DateTime in MVC 5 Edit-Modus.

    (In Show-Modus it works OK)

    What I'm doing wrong?

    My Model as follow:

    namespace WebApplication3.Models
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
    
        public partial class KEYWORD
        {
            [DisplayFormat(ApplyFormatInEditMode=true,  DataFormatString = "{0:dd.MM.yyyy}", ConvertEmptyStringToNull = true, HtmlEncode = true, NullDisplayText ="" ) ]
            public Nullable<System.DateTime> DATE_NEW { get; set; }
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}", ConvertEmptyStringToNull = true, HtmlEncode = true, NullDisplayText = "")]
            public Nullable<System.DateTime> DATE_EDIT { get; set; }



    My Edit.chtml

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        
        <div class="form-horizontal">
            <h4>KEYWORD</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.DATE_NEW, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.DATE_NEW, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.DATE_NEW, "", new { @class = "text-danger" })
                </div>
            </div>




    Make IT better.


    • Bearbeitet Interraptor Montag, 14. September 2020 14:28
    Montag, 14. September 2020 14:11

Antworten

  • Solved
            [DataType(DataType.Date)]
            [DisplayFormat(ApplyFormatInEditMode=true,  DataFormatString = "{0:dd.MM.yyyy}", ConvertEmptyStringToNull = true, HtmlEncode = true, NullDisplayText ="" ) ]
            public Nullable<System.DateTime> DATE_NEW { get; set; }
            [DataType(DataType.Date)]
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}", ConvertEmptyStringToNull = true, HtmlEncode = true, NullDisplayText = "")]
            public Nullable<System.DateTime> DATE_EDIT { get; set; }


    Make IT better.

    • Als Antwort markiert Interraptor Montag, 14. September 2020 14:45
    Montag, 14. September 2020 14:45