Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
340
How to Get DatePicker value in controller using FormCollection
posted

In HTML

@Html.Infragistics().DatePicker().ID("txtDatePicker").Render()

I have kept the datepicker controler inside the form.

In controller:

Im trying to get the value using formcollection.

public void GetFormValues(FormCollection FC)

{

 ViewData["txtDatePicker"] = FC["txtDatePicker"];

}

Only null value is return. can you help me out.

  • 10685
    Verified Answer
    Offline posted

    Hello,

    It is possible to access the igDatePicker value through the FormCollection, and yet in the context of MVC, it could be considered not the native/expected approach . To do so, it will require to manually set the InputName to be ("txtDatePicker") when instantiating the editor, because trough this collection you have access to the rendered input names and not to the id of an instantiated widget. For example:  

    In the View:
    <form action="/Home/GetDatePickerDate" method="post" name="form1">  
      <h3> Get the DatePicker Value using model </h3>
      @(Html.Infragistics().DatePickerFor(m => m.Customers.FirstOrDefault().Birthdate)
       .DataMode(DateTimeEditorDataMode.Date)
       .ID("datePicker1")
       .Render()
      )
      <br />
      <input type="submit" />
     </form>
     <br /> 
     <br />    
     <form action="/Home/GetDatePickerDateUsingFormCollection" method="post" name="form2">
      <h3> Get the DatePicker Value using FormCollection </h3>
     
      <h3> InputName "datePickerWithInputName"</h3>
      @(Html.Infragistics().DatePickerFor(m => m.Customers.FirstOrDefault().Birthdate).InputName("datePickerWithInputName")
       .DataMode(DateTimeEditorDataMode.Date)
       .ID("datePickerWithInputName")
       .Render()
      )
      <br />
      <h3> Some Comparison to native Inputs - accessible by name from FormCollection in the controller </h3> 
      @Html.CheckBox("Ckbx1")
      <input type="submit" />
     </form>
     

    However, it you could consider passing the model to the controller instead, and thus having access to the value in the same DateTime format it is bound to (assuming you have bound the igDatePicker to a model with DateTime property). 

    I am attaching a code sample where GetDatePickerDate Action will be used to access the datePicker values. You could compare it with GetDatePickerDateUsingFormCollection and see the different approach. 

                  public ActionResult GetDatePickerDateUsingFormCollection(FormCollection form2)
                 {
                         //accessing formCollection
                         string dpValue = Request.Form["datePickerWithInputName"];
                         var ckbValue = form2["Ckbx1"];
                         ViewBag.dpValueVB = dpValue;
                         ViewBag.ckbValueVB = ckbValue;
                         return View();
                  }

     
                 public ActionResult GetDatePickerDate(Customer customer)
                  {
                         string datePicker1SelectedDate = HttpContext.Request.Params.Get("Birthdate");
                         ViewBag.Date = datePicker1SelectedDate;
                         ViewBag.myCustomerModel = customer;
                         return View();
                  }

    Additionally, you could access the value through the HttpContext.Request.Params. as shown above.

    InfragisticsMvcApp1.zip