保存DateTime的问题

时间:2014-03-20 07:14:32

标签: asp.net asp.net-mvc datetime

这是我的第一个问题! 在我的项目中,当我在特殊页面中输入日期时,它被保存到数据库中,但是当我在其他页面中编辑此信息时,它不会显示,我应该再次输入有关日期的信息....

创建和编辑信息的视图:

    <div class="editor-label">
        @Html.LabelFor(model => model.DatePol)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DatePol)
        @Html.ValidationMessageFor(model => model.DatePol)
    </div>


Controller (part of edit):

 //
        // GET: /Uchet/Edit/5

        public ActionResult Edit(string userName, string nameTitle, int id = 0)
        {
            var NameLst = new List<string>();

            var NameQry = from b in db.Users
                          orderby b.Name
                          select b.Name;
            NameLst.AddRange(NameQry.Distinct());
            ViewBag.userName = new SelectList(NameLst);

            var TitleLst = new List<string>();

            var TitleQry = from r in db.Lib
                           orderby r.Title
                           select r.Title;
            TitleLst.AddRange(TitleQry.Distinct());
            ViewBag.nameTitle = new SelectList(TitleLst);




            Uchet uchet = db.Uchets.Find(id);
            if (uchet == null)
            {
                return HttpNotFound();
            }
            return View(uchet);



        }

        //
        // POST: /Uchet/Edit/5

        [HttpPost]
        public ActionResult Edit(Uchet uchet)
        {
            if (ModelState.IsValid)
            {
                db.Entry(uchet).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(uchet);
        }

1 个答案:

答案 0 :(得分:1)

试试这个

 Uchet uchet = db.Uchets.Find(id);
        if (uchet == null)
        {
            return HttpNotFound();
        }
        else
        {
             uchet.Datepol = db.tablename.FirstOrDefault(s => s.columnname == id).datetimeFieldName; //Like this you can retrive your date time in that brackets are your conditions
        }
        return View(uchet);