使用引导日期选择器选择未来日期并将其传递给 ASP.NET MVC 控制器

本文关键字:日期 ASP MVC NET 控制器 选择器 选择 未来 | 更新日期: 2023-09-27 18:35:21


我尝试将一个日期从 Bootstrap 日期选择器传递,包含在 html 形式中 ASP.NET MVC3 控制器。问题是当我选择过去日期时,传递给控制器的模型具有填充正确值的适当日期时间字段。但是,当我选择当前或将来的日期时,我在"开始日期时间"字段中得到 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>
}

控制器侧 - 开机自检方法:

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

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

使用引导日期选择器选择未来日期并将其传递给 ASP.NET MVC 控制器

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

<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属性,您会发现它将再次工作。否则,如果要更改传递的日期格式,则需要在日期字段上具有以下属性,如下所示:

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