Trung Pham
Trung Pham

Reputation: 243

ASP.NET: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Customer.Person.PersonType'

I got this error when I debugged it to the Shipping Form. Could anyone tell me where I'm wrong ??? Controller:

public ActionResult AddressAndPayment(SalesOrderHeader order,Customer customer ,FormCollection values)
        {
           // var order = new SalesOrderHeader();
            //var order1 = new SalesOrderDetail();
           // TryUpdateModel(order);
            ViewBag["PersonType"] = new SelectList(BikeDBs.Customers.Select(r => r.Person.PersonType));
       ...

View:

@Html.DisplayNameFor(model => model.Customer.Person.PersonType): 
@Html.DropDownListFor(model => model.Customer.Person.PersonType, ViewBag.PersonType as SelectList, "--Select List--",null)<br />

Upvotes: 0

Views: 87

Answers (1)

Matt Bodily
Matt Bodily

Reputation: 6413

I would highly recommend passing your select list through a view model instead of a viewbag. having said that you are setting the view bag wrong

ViewBag["PersonType"]

should be

ViewBag.PersonType

on your controller as well as your view

Upvotes: 2

Related Questions