1

Lets say my ViewModel has a property in it that is a list of Dates

IList<DateTime> concertDates;

My plan in my cshtml file is to allow the user to select multiple concert dates and when they hit a submit button, retrieve this information in the controller.

I am assuming in order to allow the user to select multiple dates I would add a DatePicker widget to the page and allow the user to select a date from it. When the user selects a date from it, the date will get added to a list in JavaScript. Then somehow this list needs to be converted to JSON and set to the ViewModel..

How would it be possible to bind the IList concertDates ViewModel property?

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • This is kinda hard to answer. So many ways you can go here. What have you tried? Sounds like you have the idea. Add some date pickers, bind some events to the datepicker changed, append to an array....ajax the array to the controller. – Botonomous Feb 02 '16 at 20:58
  • I know I can AJAX the array to the controller but lets say I have a large form on the page with Textboxes, DropDownLists, etc.. and I submit to my Save action method which accepts a ViewModel. All of the basic fields automatically get bound to the view model. How would I do the same for a completely custom scenario like this multi-date picker idea? – Blake Rivell Feb 02 '16 at 21:06
  • Use Ajax Call, get all date's in Javascript and form array or Json format, and hit the Controller, – Neo Vijay Feb 02 '16 at 21:15
  • 1
    Your simply need to dynamically generate new inputs with `` using javascript (and attach a datepicker to the new element). Ajax is not required unless you want to stay on the same page. –  Feb 02 '16 at 21:39
  • @StephenMuecke Lets say the input field ends up with values like: 1/1/2016, 1/2/2016, 1/3/2016 as the text. When the user hits submit I highly doubt MVC will automatically know that those 3 dates get bound to the IList ConcertDates property in my page's ViewModel. That is where my confusion comes in. Do I manually set Model.ConcertDates to the Textbox.Text in JS? – Blake Rivell Feb 03 '16 at 15:37
  • Did you even bother to try it. If you had, you would have realized it did work! –  Feb 03 '16 at 20:21
  • @StephenMuecke It does not work... As a simple test I added the following to my form: – Blake Rivell Feb 03 '16 at 20:26
  • 1
    NO! You create 3 inputs (using jquery), each with `` and enter a valid date in each –  Feb 03 '16 at 20:30
  • ohh!! you really need a separate input for each one? I had no idea that is how mvc worked with auto binding. I will give it a shot. Lets say in my case the user selects several dates from a calendar and I have them all stored in an array. How do I go from the dates in that array to dynamically generating multiple inputs for each date? – Blake Rivell Feb 03 '16 at 20:31
  • 1
    Note that this only works when your collection is a value type. For complex objects you need indexers in the name - refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) for more complex scenarios –  Feb 03 '16 at 20:34
  • It works! At first I was sending it using the default JavaScript dateformat but when I did .toLocaleDateString() it is sending it in as a short date and working. Thank you so much and I apologize for not figuring this out sooner. So is this basically the only way to do something like this unless I am using a 3rd party widget that knows how to bind to a List for you? – Blake Rivell Feb 03 '16 at 20:40

0 Answers0