modelBinding works when using model but not with viewmodel

MisterR

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

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")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ModelBinding on model collection

From Dev

Model.objects.get() works, but not when using a ForeignKey attribute

From Dev

Update UI when Entity Model changes in ViewModel

From Dev

MVVM - Validating the model when a field in the viewmodel is changed

From Dev

Map ICollection<Model> to List<ViewModel> using AutoMapper

From Dev

Using ReactiveUI to wire up Viewmodel and model

From Dev

"this" issue when using viewmodel in typescript and knockout

From Dev

ASP ViewModel from controller (the Model not model => model.FieldName) became null when postback to controller action

From Dev

Using only a few properties from Model in my ViewModel in MVC

From Dev

Two forms on one page, with a different model bound to each, using a ViewModel

From Dev

Two forms on one page, with a different model bound to each, using a ViewModel

From Dev

Haskell, function works when using numbers, but not with variables

From Dev

html input validation that works when using paste

From Dev

Lambda works in FindAll, but not when using it as an Func (or Expression)

From Dev

Script works in terminal but not when ran using ProcessBuilder

From Dev

Haskell, function works when using numbers, but not with variables

From Dev

MoveToElement only works when using xOffset and yOffset

From Dev

Program only works when using a decimal?

From Dev

Recursion only works when using breakpoints or getchar

From Dev

validation in model and viewmodel mvc

From Dev

Binding Model to ViewModel (WPF)

From Dev

INotifyPropertyChanged for model and viewmodel

From Dev

Validate model thru ViewModel

From Dev

Bind to Model or ViewModel

From Dev

MVVM, ViewModel, Model & MessageBoxes

From Dev

Convert Model into a ViewModel

From Dev

Creating a ViewModel from a model

From Dev

ObservableCollection in ViewModel, List in Model

From Dev

Mvvm model ViewModel

Related Related

  1. 1

    ModelBinding on model collection

  2. 2

    Model.objects.get() works, but not when using a ForeignKey attribute

  3. 3

    Update UI when Entity Model changes in ViewModel

  4. 4

    MVVM - Validating the model when a field in the viewmodel is changed

  5. 5

    Map ICollection<Model> to List<ViewModel> using AutoMapper

  6. 6

    Using ReactiveUI to wire up Viewmodel and model

  7. 7

    "this" issue when using viewmodel in typescript and knockout

  8. 8

    ASP ViewModel from controller (the Model not model => model.FieldName) became null when postback to controller action

  9. 9

    Using only a few properties from Model in my ViewModel in MVC

  10. 10

    Two forms on one page, with a different model bound to each, using a ViewModel

  11. 11

    Two forms on one page, with a different model bound to each, using a ViewModel

  12. 12

    Haskell, function works when using numbers, but not with variables

  13. 13

    html input validation that works when using paste

  14. 14

    Lambda works in FindAll, but not when using it as an Func (or Expression)

  15. 15

    Script works in terminal but not when ran using ProcessBuilder

  16. 16

    Haskell, function works when using numbers, but not with variables

  17. 17

    MoveToElement only works when using xOffset and yOffset

  18. 18

    Program only works when using a decimal?

  19. 19

    Recursion only works when using breakpoints or getchar

  20. 20

    validation in model and viewmodel mvc

  21. 21

    Binding Model to ViewModel (WPF)

  22. 22

    INotifyPropertyChanged for model and viewmodel

  23. 23

    Validate model thru ViewModel

  24. 24

    Bind to Model or ViewModel

  25. 25

    MVVM, ViewModel, Model & MessageBoxes

  26. 26

    Convert Model into a ViewModel

  27. 27

    Creating a ViewModel from a model

  28. 28

    ObservableCollection in ViewModel, List in Model

  29. 29

    Mvvm model ViewModel

HotTag

Archive