2

I have in my application a search form using AjaxBeginForm. In the same I have a date field (DateTime), and in which I am using the datePicker bootstrap.

My form looks like this:

@using (Ajax.BeginForm("Index", "Pedido", new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "divPedidos" }, new { @class = "form-inline" }))
{
    @Html.TextBox("pesquisarRascunhoId", 0, new { style = "display: none" })
    <div class="row">
        <div class="col-sm-4">
            <p>Data emissão</p>
            <div class="input-group date col-sm-5">
                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                @Html.EditorFor(model => model.PedidoFilter.EmissaoInicial, new { htmlAttributes = new { @class = "form-control input-sm datepicker" } })
            </div>
            a
            <div class="input-group date col-sm-5">
                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                @Html.EditorFor(model => model.PedidoFilter.EmissaoFinal, new { htmlAttributes = new { @class = "form-control input-sm datepicker" } })
            </div>
        </div>
    <div class="btn-search">
        <p class="demo-button">
            <button type="submit" class="btn btn-success"><i class="fa fa-search"></i><span>Pesquisar</span></button>
        </p>
    </div>
}

My javaScript for formatting my date in dd / mm / yyyy looks like this:

$(".datepicker").datepicker({
    format: "dd/mm/yyyy",
    language: "pt-BR",
    autoclose: true
});

My entity looks like this:

public class PedidoFilter
{
   public DateTime? EmissaoInicial { get; set; }
   public DateTime? EmissaoFinal { get; set; }
}

And finally my controller looks like this:

public ActionResult Index(PedidoResult pedidoResult, 
{
    var pedido = _service.FindAll(pedidoResult.PedidoFilter);

    return View(pedido);
}

I define my language culture in my Web.config like this:

<globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" />
  1. All fields of my form arrive in my controller, but my date fields are always null, in the format dd/mm/yyyy, which I define in my datepicker js.

  2. If I enter a date in the mm/dd/yyyy format it is passed to my controller.

My question is:

How can I pass my date field in the dd/mm/yyyy format and get them on my controller without them being null?

GSerg
  • 76,472
  • 17
  • 159
  • 346
mik_assad
  • 21
  • 4
  • 2
    Possible duplicate of [MVC DateTime binding with incorrect date format](https://stackoverflow.com/questions/528545/mvc-datetime-binding-with-incorrect-date-format) – Ashley Medway Aug 22 '19 at 18:43
  • What is your view model? That is what you are sending in the post – JamesS Aug 23 '19 at 09:01
  • my model is **PedidoResult** that contains a **PedidoFilter**, when my DateTime to be declare. tks – mik_assad Aug 23 '19 at 13:13
  • Decorate the DateTime field with `[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]` to set the format. I suspect it might be because the date is not the same format. – GidiBloke Aug 23 '19 at 14:04

1 Answers1

1

I think the date format can be changed with Culture or via the code example below: C# takes the first month of the year after the day. You solve this problem. Use the flatpickr or other datepicker.

<input asp-for="Birthdate" type="datetime" data-name="date-picker" class="form-control" autocomplete="off">

let birthDay = flatpickr(document.getElementById('Birthdate'), {
            enableTime: true,
            dateFormat: 'd-m-Y',
        });

   $('#myform').submit((e) => {
        $('#Birthdate').val(birthDay.latestSelectedDateObj.toLocaleString());
    });