Skip to content

Infragistics Community Forum / Web / Ignite UI for jQuery / How to Get DatePicker value in controller using FormCollection

How to Get DatePicker value in controller using FormCollection

New Discussion
Karthikeyan Rajkumar
Karthikeyan Rajkumar asked on Mar 22, 2016 2:42 PM

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.

Sign In to post a reply

Replies

  • 0
    Ivaylo Ganchev
    Ivaylo Ganchev answered on Mar 22, 2016 2:42 PM

    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

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Karthikeyan Rajkumar
Favorites
0
Replies
1
Created On
Mar 22, 2016
Last Post
9 years, 11 months ago

Suggested Discussions

Created on

Mar 22, 2016 2:42 PM

Last activity on

Feb 24, 2026 9:15 AM