Search code examples
asp.net-mvcasp.net-corerazor

Why my textBoxFor type="date" doesn't display date?


I have a simple partial view where there is a TextBoxFor type = "date" (a date picker). And when I open this partial view I would like the TextBoxFor type = "date" to display a date from the Model. But I can't reach it, the TextBoxFor displays nothing.

The Model.Dt_Reference is well displayed in the ViewBag.Title.

Here is the partial view :

    @using w_TreatmentMVC.Models
@model w_TreatmentMVC.Models.DemandeModel
@{
    Layout = null;
}
@{
    ViewBag.Title = "Modif demand n° " + Model.ID_DEMAND + " : " + Model.LI_TREATMENT + " : " + Model.DT_REFERENCE;
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<script type="text/javascript" src="../../Scripts/GlobalScripts.js"></script>

<div class="form-group">

    @Html.TextBoxFor(m => m.DT_REFERENCE, new { @type = "date", @Value = Model.DT_REFERENCE.ToString("dd/MM//yyyy") })

</div>

<div class="col-lg-12 col-md-12 col-xs-12" id="divPartial">
    <div class="col-lg-12 col-md-12 col-xs-12" id="divDemandOption">

        @Html.Action("SearchDemandOption", "Option")
    </div>
</div>
<input id="ButtonClose" type="button" value="Close" onclick="fctClose()" />

<script>

    function fctClose() {
        $("#divPopUpModal").fadeOut("slow");
        $('#divMainModal').fadeOut("slow");
    }
</script>

Could you help me ?

Thanks a lot in advance.

Eric.


Solution

  • If you want to set default value to date input,its value format need to be value="yyyy-mm-dd".And if your DT_REFERENCE is datetime type,try to do like this:

    <input type="date" asp-for="DT_REFERENCE" [email protected]_REFERENCE.ToString("yyyy-MM-dd").Split(" ")[0] />
    

    result:

    enter image description here