Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top