Datetime field is null when switching to controller

Asked

Viewed 122 times

0

I have in my application a search form using Ajaxbeginform. In the same I own a data field (Datetime), and in which I am using the bootstrap datepicker.

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 Java to format my date in dd/mm/yyyy looks like this:

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

My entity is like this:

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

And finally my controller is like this:

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

        return View(pedido);
    }

1 - All fields of my form arrive in my controller, but my date fields are always null, in format dd/mm/yyyy, defining in my datepicker js.

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

My question is:

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

  • You just need to change the application culture. You are using Owin or the settings are in the global.asax?

  • I’m not using Owin. I define my culture in my web config: <globalization uiCulture="en" Culture="en-BR" enableClientBasedCulture="true" />

1 answer

0

I believe it may be the same problem I had a short time ago, when I was going to send the value it blocked because the format is different.

You could do it this way:

$(Element).val().split('/').reverse().join('/');

That I believe solves your problem.

  • That doesn’t work for me

Browser other questions tagged

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