Search code examples
asp.netasp.net-mvc-4razoractionresulthtml.beginform

how to get date value from the @using (Html.BeginForm(


In my razor view I have a simple form that's going to accept a date from user:

@using (Html.BeginForm("CancelByDate", "ClassExample", FormMethod.Post, new { id = "dateForm" }))
   {
      <input id="dateInput" type="date" width="10" class="date" />
      <input type="submit" value="Continue" />
   }

How to get this data in my CancelByDate Action?

I have tried few ways:

    public ActionResult CancelByDate(DateTime dateInput)  // <---- This value will be null
    {
        return View();  
    }

    public ActionResult CancelByDate(String dateInput)  // <---- This value will be null
    {
        return View();  
    }

    public ActionResult CancelByDate(object dateInput) // <---- This value will be some object, but there is no way to find out what is the underlying type even with GetType(); 
    {
        return View();
    }

So I am wondering what am I missing?


Solution

  • Define a name for date like below

    <input id="dateInput" type="date" width="10" class="date" name="dateInput"/>
    

    This name should match with the parameter name in your controller action.

    If so the value will be automatically bound.