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
240
MVC for DatePicker - problem getting selected date
posted

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

Parents
  • 10685
    Offline posted

    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/

Reply Children
No Data