c# – 使用Bootstrap Datepicker选择未来日期并将其传递给ASP.NET MVC Controller

我尝试将包含在html表单中的Bootstrap datepicker的日期传递给ASP.NET MVC3控制器.事情就是当我选择过去的日期时,传递给控制器​​的模型具有填充正确值的适当的DateTime字段.但是,当我选择当前或将来的日期时,我在StartDate DateTime字段中获得0001-01-01值.
这是视图的代码:

@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>
}

控制器侧 – POST方法:

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");
    }

模型:

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; }
}

我找不到有类似问题的人.

解决方法:

从我认为问题是您的日期格式字符串在以下代码中:

<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" />

删除data-date-format html属性,你会发现它会再次运行.否则,如果您希望更改传递的日期格式,则需要在日期字段中使用以下属性,如下所示:

[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="dd-mm-yyyy")]
public DateTime StartDate { get;set; }
上一篇:java – WindowBuilder的DatePicker?


下一篇:datepicker/自定义组件/动态表单模板--数据重组