I am having problems retrieving the value of the Datepicker
Model:
public DateTime Birthdate { get; set; }
View:
@(Html.Infragistics() .DatePickerFor(m => m.Birthdate) .ID("datePicker") .Render() )
Even though I select a date from the dropdown, the date always reverts back to 1/1/0001
How can I update the Birthdate with the value selected?
Thanks
Hello,
I believe 1/1/0001 could be returned when a DataMode.Date is used but the date is actually null instead of a particular one.
There are several options to tie the igDatePicker with some data. For rendering, Let’s assume you are using a IgniteUI Razor template and have the following auto–generated code. I am adding the Birthdate prop, and extending the Customers list with the additional property details.
Models: public class Customer { public int ID { get; set; } public string Name { get; set; } public bool IsActive { get; set; } public DateTime Birthdate { get; set; } } public class CustomersViewModel { public IEnumerable<Customer> Customers { get; set; } public IEnumerable<CustomerCountSummary> CustomerCountSummaries { get; set; } } View:@using Infragistics.Web.Mvc@model InfragisticsMvcApp1.Models.CustomersViewModel
<h3> DatePickerFor Editor</h3> @(Html.Infragistics().DatePickerFor(m => m.Customers.FirstOrDefault().Birthdate) .DataMode(DateTimeEditorDataMode.Date) .ID("datePicker3") .Render() )
Controller: public ActionResult Index() { var viewModel = new CustomersViewModel { //IEnumerable of Customer Objects Customers = GetCustomers(), }; return View(viewModel);
}
private IEnumerable<Customer> GetCustomers() { return new List<Customer> { new Customer { ID=1, Name="John Smith", IsActive=true, Birthdate=new System.DateTime(2020,12,05) }, new Customer { ID=2, Name="Bob Richards", IsActive=false, Birthdate=new System.DateTime(2020,12,06) }, new Customer { ID=3, Name="Marge Wright", IsActive=false,Birthdate=new System.DateTime(2020,12,07) }, new Customer { ID=4, Name="Dwight Long", IsActive=true, Birthdate=new System.DateTime(2020,12,08) }, new Customer { ID=5, Name="Amy Grant", IsActive=true, Birthdate=new System.DateTime(2020,12,09) }, }; }
Here datePicker3 will be tied to the first Customer object and have 2020,12,05 date rendered.
Another approach could be to use a single data object/Customer and bind to Controller:Customer someCustomer = new Customer { ID = 1, Name = "John Smith", IsActive = true, Birthdate = new System.DateTime(2020, 12, 05) };return View(someCustomer);
View:@model InfragisticsMvcApp1.Models.Customer @(Html.Infragistics().DatePickerFor(m=>m.Birthdate) .DataMode(DateTimeEditorDataMode.Date) .ID("datePicker2") .Render() )
What is more, Updating the Model will be depending on your implementation and as it is more like a general MVC question, you could refer to MVC update model resources on the net. A random one for example: http://www.joe-stevens.com/2010/02/17/asp-net-mvc-using-controller-updatemodel-when-using-a-viewmodel/
Thanks for your answer but I am still having problems. Could you please attach your sample application as I want to see it in action so I can try to troubleshoot my code.
You could access the selected date via the HttpContext like string datePicker1SelectedDate = HttpContext.Request.Params.Get("Birthdate"), for example.I have used a form and a location where to post to like:
<form action="/Home/GetDatePickerDate" method="post"> <input type="submit" /> <h3> DatePickerFor Editor</h3> @(Html.Infragistics().DatePickerFor(m => m.Customers.FirstOrDefault().Birthdate) .DataMode(DateTimeEditorDataMode.Date) .ID("datePicker1") .Render() ) </form>I am attaching the code sample illustrating this approach. You could refer to it for details. What is more,I have removed Infragistics.Web.Mvc.dll from the Bin folder, so you could reference it locally to run the project.
Further, it is after the submit [HttpPost] that I look at the data returned, and this is where I see the 01/01/0001