Assign the default value to EditorFor, with current team date

0

Rescue the date with DateTime.Today or something else and assign it to the EditorFor, since the date is a string.

  <div class="form-group">
                            @Html.LabelFor(model => model.fecha, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.fecha)
                                @Html.ValidationMessageFor(model => model.fecha)
                            </div>
                        </div>
    
asked by Tomás Ignacio Torres Calderón 01.06.2017 в 05:26
source

1 answer

0

By using EditorFor you are saying that the field of type date inherits from a type of template that you have defined in Views/Shared/EditorTemplates , so you have two options:

  • You create a template in Views/Shared/EditorTemplates/Date.cshtml or Datetime.cshtml depends on the type you gave in your model with something similar to this:

    @inherits System.Web.Mvc.WebViewPage<System.DateTime?>
    
    @if (Model.HasValue)
    {
        @Html.TextBox("", String.Format("{0:dd/MM/yyyy}", Model.Value), new { @class = "form-control datepicker" })
    }
    else
    {
        @Html.TextBox("", String.Format("{0:dd/MM/yyyy}", DateTime.Now), new { @class = "form-control datepicker" })
    }
    

    This way you are declaring that all your inputs of type Date will take that style as long as you call it with the helper EditorFor

  • Pass it directly in that field with helper TextBox

    @Html.TextBox("Fecha", String.Format("{0:dd/MM/yyyy}", DateTime.Now))
    
  • answered by 01.06.2017 в 13:30