【发布时间】:2015-09-04 21:36:17
【问题描述】:
我使用VS2015中的自动脚手架工具创建了一个网站。 在 edit.cshtml 页面上,来自相关表的 2 个字段(每个字段中的主键)显示为下拉列表。我只想显示每个相关表中这些字段的值,而不是下拉列表,因为这些值不应该是可编辑的。
在控制器中,我消除了 ViewBag.(field)。在视图中,我将字段更改为@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 />
【问题讨论】:
-
你能发布视图和控制器代码吗?
-
描述是关于MVC的,但是错误看起来像是SQL问题,你能把重现问题的最少代码放在这里吗?
-
愚蠢的问题...我是 StackOverflow 的新手。我想发布我的代码,但添加为评论太长了。有没有其他方法可以做到这一点?
-
仪器型号:public partial class equipment { (...) public int id { get;放; } 公共 int tmde_id { 获取;放; } 公共 int acct_id { 获取;放; } [DisplayName("ID")] 公共字符串 instrument_id { 获取;放; } [DisplayName("S/N")] 公共字符串 serial_nbr { get;放; } 公共字符串部门 { 获取;放; } (...) 公共虚拟账户 account { get;放; } 公共虚拟 tmde tmde { 获取;放; } (...)
标签: asp.net-mvc drop-down-menu edit