Lucas Barreto
Lucas Barreto

Reputation: 150

ASP.NET MVC parameter is not being received

Very Simple question...

I have this action on the controller:

public ActionResult Index(DateTime? dataini, DateTime? datafim)
        {
            if((dataini != null) && (datafim != null))
            {
                var agendamento = db.agendamento.Where(x => x.data_agendamento >= dataini && x.data_agendamento <= datafim);
                return View(agendamento.ToList());
            }
            else
            {
                var agendamento = db.agendamento.Include(a => a.medico).Include(a => a.paciente);
                return View(agendamento.ToList());
            }


        }

And this in my view:

<p>
    @using (Html.BeginForm("Index", "agendamentos", FormMethod.Get))
    {
        <b>Data Inicial:</b>@Html.TextBox("dataini", null, new { @class = "form-control datepicker" })
        <b>Data Final:</b>@Html.TextBox("datafim", null, new { @class = "form-control datepicker" })
        <input type="submit" value="Filtrar por Data" />
    }
</p>

But i'm not receiving the parameter "datafim" from the filter. I'm only receiving the parameter "dataini". What i'm doing wrong ?

UPDATE: I forgot to put the url being passed:

http://localhost:50608/agendamentos/Index?dataini=10%2F05%2F2017+13%3A00&datafim=15%2F12%2F2018+14%3A00

UPDATE WITH THE ANSWER: I just needed to remove the FormMethod.Get of the view and everything worked out. Final code remained the same with this line changed:

@using (Html.BeginForm("Index", "agendamentos"))

Upvotes: 0

Views: 40

Answers (1)

Usman Tariq
Usman Tariq

Reputation: 43

If you see in the URL Date for datafim : 15/12/2018 and your method expecting a date which is formatted as "MM/dd/yyyy", so according to your date 15 is a day but method understands it like a month that's why its been NULL.

Upvotes: 1

Related Questions