ksalk
ksalk

Reputation: 503

Selecting future date with Bootstrap Datepicker and passing it to ASP.NET MVC Controller


I try to pass a date from Bootstrap datepicker, contained in html form to ASP.NET MVC3 controller. The thing is when I select past date, the model passed to controller has the appriopriate DateTime field filled with correct value. However when I select present or future date, I get 0001-01-01 value in StartDate DateTime field. Here's the view's code:

@using (Ajax.BeginForm("AddNewActivity", "Home", null, new AjaxOptions
{
    HttpMethod = "Post",
    UpdateTargetId = "activitiesTableDiv",
    InsertionMode = InsertionMode.Replace,
    OnBegin = "disableButton"
}))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <div class="editor-label">
            Start date
        </div>
        <div class="editor-field">
            <input id="StartDate" data-date-format="dd-mm-yyyy" class="pickDate form-control valid"
                type="text" name="StartDate" data-val-required="The StartDate field is required."
                data-val="true" />
            @Html.ValidationMessageFor(model => model.StartDate)</div>
    </fieldset>
}

Controller's side - POST Method:

public ActionResult AddNewActivity(ActivityModel model, FormCollection collection)
    {
        var test = model.StartDate;

         if (model.ObjectId == null)
            objectid = model.ObjectId;

        var jury = model.UserIds.Split(';').ExtractInt32();

        string[] emails = jury.Select(i => Storage.UserGetById(i).Email).ToArray();
        if (model.UserIds.Length > 1 && model.UserIds[model.UserIds.Length - 1] == ';')
            model.UserIds = model.UserIds.Substring(0, model.UserIds.Length - 1);

        var tags = HtmlHelperExtension.ExtractCheckListBoxResult(collection).Where(i => i.Item2).Select(i => i.Item1).ToArray();
        var groups = model.ChosenGroups.Where(i => i.Checked == true).Select(i => i.Id).ToArray();

        var url = String.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/"));
        SendActivityEmails(model.Name, url + "OIP/Activity/", emails);

        return RedirectToAction("_Activities", "OIP");
    }

Model:

public class ActivityModel
{
    public long Id { get; set; }

    public string ObjectId { get; set; }

    public TagDTO[] Tags { get; set; }

    public ModelItemChecked[] ChosenTags { get; set; }

    public DateTime StartDate { get; set; }

    public DateTime Deadline { get; set; }

    public DateTime ResultsAnnouncement { get; set; }
}

I can't find anyone having a similar problem.

Upvotes: 4

Views: 2311

Answers (1)

Inari
Inari

Reputation: 554

From what I believe the problem is your date format string in the following code:

<input id="StartDate" data-date-format="dd-mm-yyyy" class="pickDate form-control valid"
            type="text" name="StartDate" data-val-required="The StartDate field is required."
            data-val="true" />

Remove the data-date-format html attribute and you will find that it will work again. Otherwise, if you wish to change the date format that is passed, you will require the following attribute on the date field as so:

[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="dd-mm-yyyy")]
public DateTime StartDate { get;set; }

Upvotes: 3

Related Questions