我目前正在开发一个小项目(小型moviedatabase webapp),以便更好地了解asp.net应用程序。 所以我有一个视图“电影演员”。它(或更好地说数据库)包含movieID,actorID和RoleName。 movieID引用Movie Table,actorID引用Actor表。
现在,在View Movie Cast中,我有一个Dropdownlist,其中包含actor的Names(First + Lastname)。
问题:
在我编辑Moviecast的视图中,我有一个包含演员姓名的下拉列表。 让我们说角色“Bourne”的当前选定值是“Bruce Willis”(ID 1)。我打开Dropdownlist并选择一个新值“Matt Damon”(ID 2)。单击“保存”,将触发ActionResult“编辑”。这应该将MovieCast ID更新为2,但不管它总是1(或数据库中当前对应的值)
我现在尝试找错误几个小时,但没有设法解决它..
由于“Actors”表有First和Lastname的不同列,我希望Dropdownlist包含两者,我在Edit方法中手动填充
来自MovieCastController编辑方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "act_id,mov_id,role")] movie_cast movie_cast)
{
if (ModelState.IsValid)
{
db.Entry(movie_cast).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
var actors = db.actors.Select(a => new SelectListItem
{
Value = a.act_id.ToString(),
Text = a.act_firstname + " " + a.act_lastname
});
ViewBag.act_id = new SelectList(actors, "Value", "Text", movie_cast.act_id);
ViewBag.mov_id = new SelectList(db.movies, "mov_id", "mov_title", movie_cast.mov_id);
return View(movie_cast);
}
来自Edit.cshtml:
<div class="form-group">
@Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.role, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.act_id,"Actor", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("act_id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.act_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>