【发布时间】:2013-04-10 14:37:16
【问题描述】:
我真的很感激对此的一些见解:以下代码
[HttpPost]
public ActionResult Edit(BeamCollection beamcollection)
{
if (ModelState.IsValid)
{
beamcollection.BeamMaterial = db.Types.Find(Convert.ToInt32(Request.Form.Get("BeamMaterial_ID")));
db.Entry(beamcollection).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Details", "Bridge");
}
return View(beamcollection);
}
当我尝试修改 BeamCollection 记录时,所有更改都会反映并保存到数据库中,除了从 DropDownList 获取所选值的 beamcollection.BeamMaterial。调试时,我可以看到所选值被分配给 beamcollection.BeamMaterial!
顺便说一下,这个字段定义如下
public virtual AllTypes BeamMaterial { get; set; }
所以它反映了与AllTypes的一对多关系,但它是单向关系。
有点奇怪(对我来说),创建动作使用了相同的技术,而且效果很好:
public ActionResult Create(BeamCollection beamcollection)
{
if (ModelState.IsValid)
{
beamcollection.BridgeInfo = db.Bridges.Find(bridgeID);
beamcollection.BeamMaterial = db.Types.Find(Convert.ToInt32(Request.Form.Get("BeamMaterial_ID")));
db.BeamCollections.Add(beamcollection);
db.SaveChanges();
return RedirectToAction("Details", "Bridge");
}
return View(beamcollection);
}
为什么会发生这种情况以及如何使其工作,请帮助。
【问题讨论】:
标签: asp.net-mvc entity-framework entity-framework-5 html-select