发布时ASP.NET MVC日期不保存

时间:2015-09-15 14:59:37

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

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

这是表格:

这是发布的内容:

enter image description here

这是控制器:

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

2 个答案:

答案 0 :(得分:2)

您可以再次添加数据注释和调试吗?

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

答案 1 :(得分:0)

几天前我面临着类似的问题..看看下面是否有效......

使用以下attibute

修饰日期时间属性
$valueAsCents = 54780 / 100 * 100;
var_dump($valueAsCents);
var_dump((int) $valueAsCents);