我使用VS2015中的自动脚手架工具创建了一个网站。 在edit.cshtml页面上,来自相关表的两个字段(每个字段中的主键)显示为下拉列表。我只想显示每个相关表中的那些字段的值,而不是下拉列表,因为这些值不应该是可编辑的。
在控制器中,我删除了ViewBag。(字段)。在视图中,我将字段更改为@ Html.DisplayFor(...)而不是@ Html.DropDownList(...)。现在页面显示正确,但是当我保存编辑时,我收到以下错误: UPDATE语句与FOREIGN KEY约束冲突...
仪器型号:
public partial class equipment
{
(...)
public int id { get; set; }
public int tmde_id { get; set; }
public int acct_id { get; set; }
[DisplayName("ID")]
public string instrument_id { get; set; }
[DisplayName("S/N")]
public string serial_nbr { get; set; }
public string dept { get; set; }
(...)
public virtual account account { get; set; }
public virtual tmde tmde { get; set; }
(...)
控制器:
// GET: Instruments/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
equipment equipment = db.equipment.Find(id);
if (equipment == null)
{
return HttpNotFound();
}
ViewBag.acct_id = new SelectList(db.account, "id", "acct_nbr", equipment.acct_id);
ViewBag.tmde_id = new SelectList(db.tmde, "id", "noun", equipment.tmde_id);
return View(equipment);
}
// POST: Instruments/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include =
"id,tmde_id,acct_id,instrument_id,serial_nbr,dept,location,on_site,cal_cycle,cal_interval,edit_user,edit_date,active,cal_due,short_note")] equipment equipment)
{
if (ModelState.IsValid)
{
db.Entry(equipment).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.acct_id = new SelectList(db.account, "id", "acct_nbr", equipment.acct_id);
ViewBag.tmde_id = new SelectList(db.tmde, "id", "noun", equipment.tmde_id);
return View(equipment);
}
查看编辑页面:
(...)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)
@Html.LabelFor(model => model.instrument_id, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.instrument_id, new { htmlattributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.instrument_id, "", new { @class = "text-danger" })
<br />
@Html.LabelFor(model => model.account.name)
@Html.DropDownList("acct_id", null, htmlAttributes: new { @class = "form-control" })
<br />
@Html.LabelFor(model => model.tmde.noun)
@Html.DisplayFor(model => model.tmde.noun)
@Html.DropDownList("tmde_id", null, htmlAttributes: new { @class = "form-control" })
<br />
@Html.LabelFor(model => model.tmde.mfr)
@Html.DisplayFor(model => model.tmde.mfr)
<br />
@Html.LabelFor(model => model.tmde.model)
@Html.DisplayFor(model => model.tmde.model)
<br />
@Html.LabelFor(model => model.serial_nbr)
@Html.DisplayFor(model => model.serial_nbr)
<br />
答案 0 :(得分:0)
我终于想出了如何做到这一点!模特很好。以下是我所做的更改:
<强>控制器强>: 替换
ViewBag.acct_id = new SelectList(db.account, "id", "acct_nbr", equipment.acct_id);
ViewBag.tmde_id = new SelectList(db.tmde, "id", "noun", equipment.tmde_id);
return View(equipment);
。通过强>
ViewBag.acct_id = equipment.acct_id;
ViewBag.tmde_id = equipment.tmde_id;
return View(equipment);
<强> Edit.cshtml 强>: 已添加到页面顶部
@Html.HiddenFor(model => model.tmde_id)
@Html.HiddenFor(model => model.acct_id)
在我显示各个字段的正文中:
<dt>@Html.LabelFor(model => model.tmde.noun)</dt>
<dd>@Html.DisplayFor(model => model.tmde.noun)</dd>
我可以显示设备表中的任何字段以及相关表格。
此代码确保维护外键(隐藏,因为它们不能由用户编辑),同时仍允许编辑设备表中的其他字段。现在数据库UPDATE完美运行。
希望这会帮助可能遇到相同情况的其他人。