我有点尴尬地问这个,因为在ASP / MVC中相当新,我使用内置的脚手架控制器/ Visual Studio视图开始了我的项目。但是,我在网上找不到与我相同的问题所以我可能错过了一些非常基本的东西,感谢理解
我还没有触及控制器中的脚手架编辑操作中的任何内容:
控制器/修改
// GET: Tenders/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tender tender = db.Tenders.Find(id);
if (tender == null)
{
return HttpNotFound();
}
ViewBag.CompendiumID = new SelectList(db.Compendia, "ID", "CTPR", tender.CompendiumID);
ViewBag.TrainerRegistrationID = new SelectList(db.TrainerRegistrations, "ID", "NTTC", tender.TrainerRegistrationID);
return View(tender);
}
模型(部分)
public int Slots { get; set; }
[DataType(DataType.Date)]
[DisplayName("Training Start")]
[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}", ApplyFormatInEditMode = true)]
public DateTime TrainingStart { get; set; }
[DataType(DataType.Date)]
[DisplayName("Training End")]
[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}", ApplyFormatInEditMode = true)]
public DateTime TrainingEnd { get; set; }
查看(部分)
<div class="form-group">
@Html.LabelFor(model => model.Slots, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Slots, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Slots, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TrainingStart, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TrainingStart, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TrainingStart, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TrainingEnd, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TrainingEnd, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TrainingEnd, "", new { @class = "text-danger" })
</div>
</div>
点击修改
后会发生什么的屏幕截图如上所述,所有数据都可以插入并存储在数据库中而不会遇到任何问题,但是当涉及到编辑时,包含DateTime属性的字段将在表单中重置,尽管取消编辑不会发生变化任何先前存储的数据。但是,当我真的想要更新一行时,它变得很麻烦,因为我必须重新做日期。这真的是脚手架形式默认是如何工作的吗?还是我在某个地方弄错了?请帮忙