Datapicker com @Html.Editorfor

Asked

Viewed 40 times

0

I have the following model

    [DisplayName("Data Inicial Contrato")]
    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime DataInicialContrato { get; set; }

    [DisplayName("Data Final Contrato")]
    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime DataFinalContato { get; set; }  

View:

 <div class="form-group">

            <div class="col-md-3 col-sm-12">
                @Html.LabelFor(model => model.DataInicialContrato , htmlAttributes: new { @class = "control-label", @for = "datainicialcontrato" })
                @Html.EditorFor(model => model.DataInicialContrato, new { htmlAttributes = new { @class = "form-control ", @id = "datainicialcontrato"  } })
                <span class="help-block">@Html.ValidationMessageFor(model => model.DataInicialContrato, "", new { @class = "text-danger" })</span>
            </div>

            <div class="col-md-3 col-sm-12">
                @Html.LabelFor(model => model.DataFinalContato , htmlAttributes: new { @class = "control-label", @for = "datafinalcontrato" })
                @Html.EditorFor(model => model.DataFinalContato, new { htmlAttributes = new { @class = "form-control", @id = "datafinalcontrato" } })
                <span class="help-block">@Html.ValidationMessageFor(model => model.DataFinalContato, "", new { @class = "text-danger" })</span>
            </div>

 </div>

The datapicker is not showing up. I need a DD/MM/YYYY datapicker to appear and be in English.

  • Nothing appears? What about the generated HTML? I just copied your code for this example and worked normally.

1 answer

2


For this you can use the Datepicker Bootstrap as follows:

<div class="col-md-3 col-sm-12">
     @Html.LabelFor(model => model.DataInicialContrato , htmlAttributes: new { @class = "control-label", @for = "datainicialcontrato" })
     @Html.EditorFor(model => model.DataInicialContrato, new { htmlAttributes = new { @class = "form-control datepicker", @id = "datainicialcontrato"  } })
     <span class="help-block">@Html.ValidationMessageFor(model => model.DataInicialContrato, "", new { @class = "text-danger" })</span>
</div>

And in the javascript:

<script type="text/javascript">
    $(function () { 
       $('.datepicker').datepicker();
    });
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.