Display a formatted date in an EditorFor()

Display a formatted date in an EditorFor()

Asked on December 27, 2018 in C#.
Add Comment


  • 6 Answer(s)

    Try to use this code,

    @Html.TextBoxFor(m => m.StartDate,
       new { @Value = Model.StartDate.ToString("yyyy/MM/dd"), @class="datepicker" })
    
    Answered on December 27, 2018.
    Add Comment

    The question is about EditorFor(), but in the actual code is been used TextboxFor().

    In the Model, we have

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

    In the View, one can use this code.

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

    It should be fine.

    One can reuse the above model somewhere else and do not have to specify the method of display the date in the View. We have to change the display format, and change it once a time.

    Answered on December 27, 2018.
    Add Comment

    This is very simple when we use the type attribute in the code,

    @Html.EditorFor(model => model.FechaRegistro, new { htmlAttributes = new { @class = "form-control oso" ,@type = "date" } })
    
    Answered on December 27, 2018.
    Add Comment

    I was told to use the Metadata files so eventually I changed the meta data files as below

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    using System.Linq;
    using System.Web.Mvc;
    
    namespace VFS_ProcurementTracking.Models
    {
        [MetadataType(typeof(prc_RequirementMetaData))]
        public partial class prc_Requirement
        {
            internal sealed class prc_RequirementMetaData
            {
                private prc_RequirementMetaData()
                {
                }
            }
    
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
            public Nullable<System.DateTime> CustomerRDD { get; set; }
    
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
            public System.DateTime Import_CreateDate { get; set; }
        }
    }
    

    Answered on January 15, 2019.
    Add Comment

    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.

    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
        {
    
        }
    }
    Answered on January 15, 2019.
    Add Comment
    public class DateTimeModelBinder : IModelBinder
        {
            public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                var incomingData = bindingContext.ValueProvider.GetValue("date").AttemptedValue;
                int day = Convert.ToInt32(incomingData.Split(new char[1] { '/' })[0]);
                int month = Convert.ToInt32(incomingData.Split(new char[1] { '/' })[1]);
                int year = Convert.ToInt32(incomingData.Split(new char[1] { '/' })[2]);
                return new DateTime(year,month,day);
            }
        }

     

    Answered on January 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.