0

In my program, I have implemented multidatespicker (http://multidatespickr.sourceforge.net/) for my solution. I have an attribute called DatesDispo to receive my dates picked from my calendar.

public List<DateTime> DatesDispo { get; set; }

In my razor view, I have an input for my calendar:

<div class="form-group">
    @Html.LabelFor(model => model.DatesDispo, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <input name="DatesDispo" id="DatesDispo"></input>
    </div>
</div>

I see my calendar and until there it's working. But on post of the form, I don't receive my dates that I picked in my DateDispo field. In debug mode, I had tested this and it worked but maybe I changed something and now I'm unable to bind dates picked with my date field. Someone know why? I swear that I had picked 2 dates and my list of dates had a count = 2.

Edited: Data received from post => https://gyazo.com/629a7f01c6802b2cefc825d786b2470b

__RequestVerificationToken=FG381kMw0ANq_vV0ucoFr7Rrn6J1nqZNptlSy9JtT27JUggqOvru9Q‌​FkPZP40QniZ_ZBtpBNzMN00MZA5Aq06ZByz3yAz9-YNXpNjMhOG0o1&IdHabilete=7&Adresse=&Vill‌​e=&CodePostal=&Pays=&Province=&DateMax=2016-01-26+13%3A42%3A23&DateCreation=2016-‌​01-26+13%3A42%3A23&DatesDispo=01%2F28%2F2016%2C+01%2F29%2F2016&Specifications=Sys‌​tem.Collections.Generic.List%601%5BFavornet.Models.Biz.SpecDemande%5D DatesDispo=01%2F28%2F2016%2C+01%2F29%2F2016

DatesDispo field extracted from above

DatesDispo=01%2F28%2F2016%2C+01%2F29%2F2016
Jasen
  • 13,170
  • 3
  • 43
  • 64
  • Use a network monitor tool (in your browser's debugger or Fiddler) to watch the POST request. What does your dates look like in the request body? What does your post action signature look like? – Jasen Jan 26 '16 at 18:40
  • This? https://gyazo.com/629a7f01c6802b2cefc825d786b2470b – Émile Pettersen-Coulombe Jan 26 '16 at 18:46
  • Yes, and click _view source_ to see the raw data sent. The tool may be showing you a "friendly view" of the data. The automatic binding may have problems with the date format sent and possibly your locale setting. – Jasen Jan 26 '16 at 19:00
  • __RequestVerificationToken=FG381kMw0ANq_vV0ucoFr7Rrn6J1nqZNptlSy9JtT27JUggqOvru9QFkPZP40QniZ_ZBtpBNzMN00MZA5Aq06ZByz3yAz9-YNXpNjMhOG0o1&IdHabilete=7&Adresse=&Ville=&CodePostal=&Pays=&Province=&DateMax=2016-01-26+13%3A42%3A23&DateCreation=2016-01-26+13%3A42%3A23&DatesDispo=01%2F28%2F2016%2C+01%2F29%2F2016&Specifications=System.Collections.Generic.List%601%5BFavornet.Models.Biz.SpecDemande%5D – Émile Pettersen-Coulombe Jan 26 '16 at 19:07
  • Easier to see : DatesDispo=01%2F28%2F2016%2C+01%2F29%2F2016 – Émile Pettersen-Coulombe Jan 26 '16 at 19:10
  • Can you explain to me? haha I'm not sure what is the problem – Émile Pettersen-Coulombe Jan 26 '16 at 19:19
  • I see possibly three issues: 1) The dates are sent as single string and the auto binding doesn't recognize it as a collection of dates. 2) If you [post a collection](http://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects) you usually need an index. 3) Your machine's locale setting may have trouble parsing the date string with the format the client-side is sending mm/dd/yyyy. It will also help to see your action function. – Jasen Jan 26 '16 at 19:41
  • I don't know how I did it but check this, I have 1 date in my list: https://gyazo.com/9974320fcb66b4cfa08d25ebef7ebe0c – Émile Pettersen-Coulombe Jan 26 '16 at 19:49
  • But 2 dates is not working – Émile Pettersen-Coulombe Jan 26 '16 at 19:50
  • You will either need to accept the dates as a comma-delimited string of dates. Then parse that string on the server back into an array of date strings. Then into a collection of DateTime objects. OR it looks like you need an index on your `DatesDispo` input(s). `` which then leads to a new problem of generating multiple inputs dynamically. – Jasen Jan 26 '16 at 20:01
  • 1
    I finally took the string and convert it to dates. – Émile Pettersen-Coulombe Jan 26 '16 at 20:15
  • Yes, that's one way to do it. – Jasen Jan 26 '16 at 20:16

1 Answers1

0

Although you intend to post a collection of dates. You are posting a single field as a single string. In this case the auto binding doesn't have a problem with a single date but can't recognize the way you send multiple dates.

<input name="DatesDispo" id="DatesDispo"></input>

A single input is sending as

DatesDispo=01%2F28%2F2016%2C+01%2F29%2F2016

Or

"01/28/2016, 01/29/2016"

One solution is to send it as string that needs to be parsed back into a collection.

public class SomeModel
{
    public string DatesDispo { get; set; }
}

[HttpPost]
public ActionResult SendDates(SomeModel model)
{
    List<DateTime> dates = new List<DateTime>();
    var datesDispo = model.DatesDispo.Split(',');

    foreach(var date in datesDispo)
    {
        dates.Add(DateTime.Parse(date));
    }

    ...
}
Jasen
  • 13,170
  • 3
  • 43
  • 64