1
votes

I have a partial view with some controls in it. One of them is a textbox that is used to enter a DateTime value in it. The datetime textbox is readonly, and the type attribute is text (to prevent the browser date selector popup from being shown). The DateTime property in the model class in not nulable and unique. It cannot be nullable.

Since the textbox is read only, the DateTime value is entered using the jQuery Datepicker with the timepicker addon, by clicking in the textbox and selecting the relevant date in datepicker popup.

Important: Although the partial view is embedded in the normal view, it is not rendered in it. The partial view, which is actually a form, is displayed in a jQuery modal when the user clicks an action link on the normal view. The partial view is embedded as an Html.Action method.

Everything works as it should. My problem is, however, when the partial view is shown in the jQuery modal, the DateTime text box is shown with the placeholder text Monday 01, January, 0001.

On other pages in the application with DateTime controls in them, all I needed to do was change the type attribute to “text” and add an HTML placeholder by @placeholder ="Select date", But this did not work.

Any assistance would be appreciated

Edited the title : Changed "placeholder" to "default value"

Model

    [Index(IsUnique = true)] 
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dddd dd, MMMM, yyyy HH:mm}", ApplyFormatInEditMode = true)]
    [Display(Name = "Record Date")]
    public DateTime RecordDate { get; set; }

Controller

    //[HttpPost]
    //[ValidateAntiForgeryToken]
    public ActionResult CreateNewRecordOnPopup([Bind(Include = "RecordID,RecordDate,TeamPeriodID,TeamOnDutyID")] Record record)
    {
        ViewBag.TeamOnDutyID = new SelectList(db.TeamOnDutys, "TeamOnDutyID", "Name");

        ViewBag.TeamPeriodId = new SelectList(db.TeamPeriods, "TeamPeriodID", "Name");
        return PartialView("_CreateNewRecordOnPopupPV", record);
    }

Partial View in Normal View

@Html.Action("CreateNewRecordOnPopup", "Records")

Partial View

@Html.EditorFor(model => model.RecordDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @type = "text", placeholder = "Select date" } })

1

1 Answers

5
votes

Your DateTime property is non-nullable, so it comes with a default value of default(DateTime), which is 1/1/0001 12:00:00 AM. It's not a placeholder. It's literally the initial value because it has to be set to something.

To get rid of this, make your DateTime nullable, i.e. DateTime?. You can still enforce that it be set to something by decorating it with [Required] if you need to. However, since it's nullable, it's default value will be null or an empty string in the input.