I am currently working on a small project(small moviedatabase webapp) to get a better understanding of asp.net applications. So I have a View "Movie Cast". It(or better said the database) contains a movieID,actorID and a RoleName. The movieID references to the Movie Table and the actorID to the Actor table.
Now in the View Movie Cast I have a Dropdownlist that contains the Names(First + Lastname) of the actors.
The Problem:
In my View to Edit the Moviecast I have a Dropdownlist that contains the actors names. Lets say the currenty selected value for role "Bourne" is "Bruce Willis"(ID 1). I open the Dropdownlist and select a new value "Matt Damon"(ID 2). Click Save and the ActionResult "Edit" gets fired. This should update the MovieCast ID to be 2 but nomatter what it is always 1(or the current corresponding value in the database)
I tried finding the error for some hours now but didnt manage to fix it..
Since the table "Actors" has different columns for First and Lastname, and I want the Dropdownlist to contain both, I populated it in the Edit method manualy
From MovieCastController Edit Method:
[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);
}
From 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>