0

I'm having issues with drop down lists on MVC. I've searched and searched but to no avail.

The issue

My ViewModel

public class IncidentFormViewModel
{
    public Guid Guid { get; set; }

    public Incident Incident { get; set; }

    public Guid PersonInvolvedId { get; set; }

    public IEnumerable<Person> People { get; set; }
}

My Controller

public ActionResult New()
{
    var incidentFormVM = new IncidentFormViewModel
    {
        Incident = new Incident(),
        People = unitofwork.Person.GetAll()
    };            
    return View("IncidentForm", incidentFormVM);
}

and View(I've taken out useless information)

@using (Html.BeginForm("Save", "Incident"))

<div class="container">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                @Html.LabelFor(m => m.Incident.PersonInvolved)
                @Html.DropDownListFor(m => m.PersonInvolvedId, new SelectList(Model.People, "Id", "FirstName"), new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.Incident.PersonInvolved)
            </div>
        </div>
        <br />

        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</div>

The thing is if i put a stop on the line below and put a watch on Model.People i can see the list of people against it.

@Html.DropDownListFor(m => m.PersonInvolvedId, new SelectList(Model.People, "Id", "FirstName"), new { @class = "form-control" })

What am i doing wrong?

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • Is this happening when the page is first rendered, or just when you submit (and as a side note, your `LabelFor()` and `ValidationMessageFor()` are for a different property - and therefore a bot pointless) –  Apr 23 '18 at 22:33
  • Hi Stephen, The page loads fine and i get all "Persons" from my database loaded into the Drop down box its only when i submit it throws this error. – Ryan Stuart Apr 23 '18 at 22:36
  • Because you did not re-populate `People` when you return the view (therefore it is `null`) –  Apr 23 '18 at 22:38
  • And as a side note, view models do not contain data models - they contain the properties of `Incident` that you want in the view (plus an `IEnumerable` property for the options) –  Apr 23 '18 at 22:40
  • Hi Stephen, excuse me if i'm not picking you up correctly i've just started MVC. You say that i dont re-populate People when i return the view which makes it null on submit which makes sense but its not clear to me how to re-populate people - Could you point me in the right direction. Also You said as a side note that view models do not contain data models am i correct it thinking then i should totally ditch public IEnumerable People { get; set; } and instead replaceit with IEnumerable which i would populate with my "Person" GUID and Name ? – Ryan Stuart Apr 23 '18 at 22:54
  • 1
    In your POST method you need to do `model.People = unitofwork.Person.GetAll()` just as you did in the GET method when `ModelState` is invalid and you return the view (suggest you also read [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Apr 23 '18 at 22:57
  • Also read [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Apr 23 '18 at 22:58
  • Thanks very much i'll check them out. I understand my problem now... Don't know how i didn't realize it sooner. Thanks. – Ryan Stuart Apr 23 '18 at 23:13
  • Probably also helpful to read up on the PRG pattern: https://en.wikipedia.org/wiki/Post/Redirect/Get – Tieson T. Apr 24 '18 at 01:47

0 Answers0