我的第一个问题!在我的项目中,当我在特殊页面输入日期时,它被保存到数据库,但当我在其他页面编辑此信息时,它不显示,我应该再次输入有关日期的信息....
创建和编辑信息的视图:
<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);
}
Try like This
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);