Question

We are still busy with our project for school and we have some problems.

We have a memberclass and a friendclass In the friendclass, we have memberID & FriendID as a composite Primary Key. So memberID refers to the MemberID from the memberclass but FriendID also refers to this memberID from the memberclass because a Friend must be a member.

So in our views we can chose from a dropdownlist wich member can be friend with another member and storage that.

But if we want to edit this friendship and want to change the friend of a member or change both names. We can not see the current friend in the dropdownlist although we can see the member who's friendship we want to change in the dropdownlist.

here are a few screenshots:

The first screenshot is the index view that gives us the ID's from the member and the friend (who's also a member) so the table names are: Person - Friend with

enter image description here

If we now watch the edit view we should see Person: Jan (PersonID = 1) and vriend met (friend with): Jeanine (PersonID2)

enter image description here

So we can already get the name of member one (Jan) by using a viewbag and adding it to a view:

 public ActionResult Edit(int id, int idTwo)
    {
        ViewBag.PersoonID = new SelectList(PersoonBLL.GetAll(), "PersoonID", "Naam");
      ViewBag.VriendID = new SelectList(PersoonBLL.GetAll(), PersoonBLL.GetById(idTwo).Naam.ToString());
        //ViewBag.VriendID = new SelectList(vriendschapBLL.GetAll(), "VriendID", "PersoonBLL.GetAll().Naam");

        Vriendschap vriendschap = vriendschapBLL.GetByTwoId(id, idTwo);
        if (vriendschap == null)
        {
            return HttpNotFound();
        }
        return View(vriendschap);
    }

This is the code in the view:

<div class="editor-label">
        <%: Html.LabelFor(model => model.PersoonID) %>
    </div>
    <div class="editor-field">
        <%: Html.DropDownList("PersoonID") %>
        <%: Html.ValidationMessageFor(model => model.PersoonID) %>
    </div>


            <div class="editor-label">
        <%: Html.LabelFor(model => model.VriendID) %>
    </div>
    <div class="editor-field">
        <%: Html.DropDownList("VriendID") %>
        <%: Html.ValidationMessageFor(model => model.VriendID) %>
    </div>

Originally we had the index of member 2 which was right but we wanted to have the name of member 2 instead of the index. It must be something in our controller with the viewbag but, we want to have the VriendID (friendID) to be "Naam" (name) wich is not in a field in our vriendschap table (friendship) but in the memberTable/class

So to make this clear: If you see the code from the viewbag how we did viewbag.PersoonID we want something similar for VriendID (friendID) but there is no field called "Naam" (name) in the friendshipTable only 2 keys, the field "Naam" comes from the PersoonTable (persontable) wich the first viewbag refers to. If we did the same for viewbag.VriendId we got two times the same name.

Sorry if this is very complicated, it's not easy to explain this in English if we use dutch names. Thank you in advance!!

Was it helpful?

Solution

You Should use:

ViewBag.VriendID = new SelectList(PersoonBLL.GetAll(), "PersoonID", "Naam",idTwo);

This should do it. idTwo will take the next Id from this list of persons that you actually used.

Hope this works for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top