date in javascript wrong format for controller c# (Asp net mvc)

Asked

Viewed 192 times

-1

I’m in a strange situation.

I am working with datepicker jquery, asp net mvc 5.

i configured the datepicker for the format as below:

 $(".datepicker").datepicker({
        autoclose: true,
        buttonImageOnly: true,
        showAnim: 'slideDown',
        duration: 'fast',
        buttonText: "Calendário",
        dateFormat: 'dd/mm/yy',
        showOn: "both",
        buttonImage: "../Content/images/calendario.png",          
        clearBtn: true,
        dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado', 'Domingo'],
        dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S', 'D'],
        dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb', 'Dom'],
        monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
        monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
        onSelect: function () {
            $(this).removeClass('input-validation-error');
            $('#dataDeInicio-error').hide();
        }
    });

It comes from a multiselect:

@Html.TextBoxFor(model => model.DataDeInicioDaQuestao, "{0:dd/MM/yyyy}", new { @Value = Model.DataDeInicioDaQuestao.HasValue ? Model.DataDeInicioDaQuestao.Value.ToString("dd/MM/yyyy") : String.Empty, @class = "datepicker campoData grid_2", @id = "DataDeInicioDaQuestao", maxlength = "10", @readonly = "readonly" })

But when I give Ubmit in the form it sends to Actionresult the date in wrong format. So, for example, if I put the date 18/07/2019 it considers 07/18/2019 and the error occurs.

public ActionResult ObterRelatorioDeRelatorioDePercentualDeAcertosDaQuestao(DateTime? dataDeInicioDaQuestao, DateTime? dataDeFimDaQuestao, string questao)
    {
        try
        {}
}

what I do????

  • first change your parameters of DateTime? for string and see the format that is coming up. Dai can easily format in C#

  • as I didn’t think of it, right? It worked now. Then it was just convert the string to date. Thank you so much!

  • good, dates are always a problem.. another solution would be to use the library Moment.js and convert the format in javascript, have some examples here on the site, works well

1 answer

0

Your model

  [Required(ErrorMessage = "informe sua data.")]
  [DataType(DataType.Date)]
  public DateTime IssueDate { get; set; }

Razor Page

 @Html.TextBoxFor(model => model.IssueDate)
 @Html.ValidationMessageFor(model => model.IssueDate)

Jquery Datepicker

<script type="text/javascript">
    $(document).ready(function () {
        $('#IssueDate').datepicker({
            dateFormat: "dd/mm/yy",
            showStatus: true,
            showWeeks: true,
            currentText: 'Now',
            autoSize: true,
            gotoCurrent: true,
            showAnim: 'blind',
            highlightWeek: true
        });
    });
</script>

Webconfig file

<system.web>
    <globalization uiCulture="en" culture="en-GB"/>
</system.web>

Now your text box will accept the format "dd / MM / yyyy" .

  • that way unfortunately it didn’t work :( but thanks for the help! what I did was put the action date parameter as string and then convert it to datetime . Then it worked!

Browser other questions tagged

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