0

I'm trying to get a dropdown show the right value when editing using a viewmodel but it only works when i pass the complete model to the view.

When I do it like this and there is already a contact selected it shows that contact in the edit screen.

Model

public class ClientModel
{
  public int ID { get; set; }
  public int ContactID { get; set; }
  //Other atributes
}

View EditContact

@model Project.Models.ClientModel
@Html.DropDownListFor(model => model.ContactID , (SelectList)ViewBag.ContactID, "select a contact")

Controller

   public ActionResult EditContact(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var Contact = db.Contacts.ToList();
        ViewBagID.Contact = new SelectList(Contact.AsEnumerable(), "ID", "name", "Contact");
        ClientModel model= db.ClientModel.Find(id);

        return View(model);
    }

But when I do it like this and there is already a contact selected the dropdownlist shows select contact.

Model

public class ClientModel
{
  public int ID { get; set; }
  public int ContactID { get; set; }
  //Other atributes
}

ViewModel

public class ClientEditContactModel
{
  public int ID { get; set; }
  public int ContactID { get; set; }
}

View EditContact

@model Project.Models.ClientEditContactModel
@Html.DropDownListFor(model => model.ContactID, (SelectList)ViewBag.ContactID, "select a contact")

Controller

   public ActionResult EditContact(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var Contact = db.Contacts.ToList();
        ViewBag.ContactID = new SelectList(Contact.AsEnumerable(), "ID", "name", "Contact");
        ClientModel client= db.ClientModel.Find(id);
        ClientEditContactModel model = new ClientEditContactModel();

        model.ID = client.ID;
        model.ContactID = client.ContactID 

        return View(model);
    }

How do i fix this with the viewmodel?

Edit I've made some typo's in my code so I fixed them but because of them i found the answer see below.

MisterR
  • 21
  • 7

2 Answers2

1

I found the answer after some more research here https://stackoverflow.com/a/11949123/4252392.

The problem was that ViewBag's name is the same as the model's property. So i changed the Viewbag's name.

New Controller

 public ActionResult EditContact(int? id)
 {
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var Contact = db.Contacts.ToList();
    ViewBag.ContactIDList = new SelectList(Contact.AsEnumerable(), "ID", 
                                           "name", "Contact");
    ClientModel client= db.ClientModel.Find(id);
    ClientEditContactModel model = new ClientEditContactModel();

    model.ID = client.ID;
    model.ContactID = client.ContactID 

    return View(model);
}

New View

@model Project.Models.ClientEditContactModel
@Html.DropDownListFor(model => model.ContactID, (SelectList)ViewBag.ContactIDList, 
                                "select a contact")
Community
  • 1
  • 1
MisterR
  • 21
  • 7
  • Note, it should be `ViewBag.ContactIDList = new SelectList(Contact, "ID", "name")` Remove the last parameter. And you do not need `AsEnumerable()` - its already `IEnumerable`! (I assume `PurchaseContact` should be `Contact` or else its `var PurchaseContact = db.Contacts;` - again no need to use `.ToList()`) –  Dec 13 '14 at 01:04
0

If you set selected value in ContactID property from dropdown so you need to set dropdown in view like below:

@model Project.Models.ClientEditContactModel
@Html.DropDownListFor(model => model.ContactID, (SelectList)ViewBag.Contact,
                      "select a contact")
Pragnesh Khalas
  • 2,908
  • 2
  • 13
  • 26
  • I just noticed I did it like that in the code but didn't put it right in the question. But it still doesn't work with the viewmodel. – MisterR Dec 12 '14 at 13:10
  • Just confirm that after change the code is any value is selected in dropdown or not or just displayed “select a contact”? – Pragnesh Khalas Dec 12 '14 at 13:18
  • I found the solution and fixed my question. I hope it is clear now. Made some typos. My apologies for that. – MisterR Dec 12 '14 at 13:44