【问题标题】:ASP.NET MVC date not saving when posting发布时未保存 ASP.NET MVC 日期
【发布时间】:2015-09-15 14:59:37
【问题描述】:

表单上的某些日期存在问题,无法回发。我在字段中输入了一个日期,但该应用程序似乎返回了一个空白的 DateTime -“01/01/0001 00:00:00”

这是表格:

这是发布的内容:

这是控制器:

    public ActionResult Add()
    {
        var skillsetIDs = db.SkillSets.Select(x => x.IDSkillset).Distinct();
        List<SelectListItem> items = new List<SelectListItem>();
        foreach (var t in skillsetIDs)
        {
            SelectListItem s = new SelectListItem();
            int catID = db.SkillSets.Where(c => c.IDSkillset == t).Select(x => x.IDCategory).Single();
            string product = db.SkillSets.Where(c => c.IDSkillset == t).Select(x => x.Product + " V. " + x.P_Version).Single();
            string category = db.Categories.Where(c => c.IDCategory == catID).Select(x => x.Category + ": " + x.C_Role + " - ").Single();
            s.Text = category + product;
            s.Value = t.ToString();
            items.Add(s);
        }
        ViewBag.Campaign = items;

        var personIDs = db.Personnel.Select(x => x.IDPerson).Distinct();
        List<SelectListItem> items2 = new List<SelectListItem>();
        foreach (var t in personIDs)
        {
            SelectListItem s = new SelectListItem();
            s.Text = db.Personnel.Where(c => c.IDPerson == t).Select(x => x.Fornames + " " + x.Surname).Single();
            s.Value = t.ToString();
            items2.Add(s);
        }
        ViewBag.Person = items2;
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Add(Models.PersonSkillsModel model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.PersonSkills.Add(model);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

观点:

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>PersonSkillsModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.IDSkillSet, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(m => m.IDPerson, (IEnumerable<SelectListItem>)ViewBag.Person)
                @Html.ValidationMessageFor(model => model.IDSkillSet, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IDSkillSet, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(m => m.IDSkillSet, (IEnumerable<SelectListItem>)ViewBag.Campaign)
                @Html.ValidationMessageFor(model => model.IDSkillSet, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Score, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Score, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Score, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ScoreDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ScoreDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ScoreDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TargetScore, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TargetScore, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TargetScore, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TargetDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TargetDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TargetDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RefresherDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RefresherDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RefresherDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

型号:

[Table("PersonSkills")]
public class PersonSkillsModel
{
    [Key]
    public int PersonSkillsID { get; set; }

    public int IDPerson { get; set; }

    public int IDSkillSet { get; set; }

    public int Score { get; set; }

    public DateTime ScoreDate { get; set; }

    public int TargetScore { get; set; }

    public DateTime TargetDate { get; set; }

    public DateTime RefresherDate { get; set; }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    能否添加数据注释并再次调试?

    [DataType(DataType.Date)]
    public DateTime ScoreDate { get; set; }
    

    【讨论】:

    • @TheHornyDonut,这与解决您的问题无关。该属性所做的只是添加 type="date" 属性,以便它呈现浏览器 HTML5 日期选择器,该日期选择器以 ISO 格式返回值。它仅在 Chome 中受支持,因此在 IE 或 FireFox 中仍然会失败。您的问题很可能是文化问题。最重要的是,如果 ModelState 无效,您需要 return View(model) (不是 return RedirectToAction("Index");),这样您就可以看到发生了什么。
    【解决方案2】:

    几天前我遇到了类似的问题..看看下面的一个是否有效..

    使用以下属性装饰日期时间属性

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MMMMM-dd}")]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多