0

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>
Constantin M
  • 119
  • 1
  • 4
  • 18
  • Its not clear what you issue is (and what in the world is `movie_cast.mov_id = (int)Session["movid"];` etc for ? - no matter what you select in the dropdownlist, your just overwrite it with what ever the value in `Session` is) –  Apr 14 '18 at 11:34
  • Okay sorry, I will try to formulate the question better! I allready commented those lines out, when I realised that I dont need to store the ids in a session. But it didnt fix the problem. I will update my post! – Constantin M Apr 14 '18 at 11:39
  • Are you actually hitting `db.SaveChanges();` and redirecting (i.e. is `ModelState` valid)? –  Apr 14 '18 at 11:51
  • Yes I am. Its actually that the "movie_cast" object that gets passed to the "Edit" Method already contains the "wrong" id. – Constantin M Apr 14 '18 at 11:57
  • Presumably because you do not have a form control for it :). But before you write any more code - 1. You are editing data - ALWAYS use a [view model](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) and never `ViewBag` or a `[Bind]` attribute 2. Your properties will be `int ID`, `int? SelectedRole`, `int? SelectedActor`, `IEnumerable RoleList` and `IEnumerable ActorList` (and you will not need an input for the ID because it will be automatically bound. –  Apr 14 '18 at 12:04
  • 1
    And use `@Html.DropDownListFor(m => m.SelectedRole, Model.RoleList, ....` to generate your dropdownlists –  Apr 14 '18 at 12:05
  • @ConstantinM, you populate ViewBag with list items for the dropdown, but why list not being passed to the Html helper in your view? Apparently is getting null `@Html.DropDownList("act_id", null, htmlAttributes:` Can you explain? – derloopkat Apr 14 '18 at 12:26

0 Answers0