0

Having an issue with some dates on a form not posting back. I am entering a date into the fields, but the app seems to be posting a blank DateTime back - "01/01/0001 00:00:00"

This is the form:

This is what is being posted:

enter image description here

This is the controller:

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

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

The model:

[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; }
}
TheHornyDonut
  • 127
  • 1
  • 2
  • 12

2 Answers2

2

Could you add Data Annotation and debug again?

[DataType(DataType.Date)]
public DateTime ScoreDate { get; set; }
Win
  • 61,100
  • 13
  • 102
  • 181
  • @TheHornyDonut, This has nothing to do with solving your issue. All the attribute does is add the `type="date"` attribute so it renders the browsers HTML5 datepicker which posts the value back in ISO format. Its only supported in Chome so will still fail in IE or FireFox. Your issue is most likely a culture issue. Most importantly you need `return View(model) ` (not `return RedirectToAction("Index");`) if `ModelState` is invalid so you can see whats going on. –  Sep 15 '15 at 22:35
0

I was facing similar issue few days back.. see if below one works..

decorate datetime properties with following attibute

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MMMMM-dd}")]
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48