Illep
Illep

Reputation: 16841

There is no ViewData item of type 'IEnumerable' that has the key

I am getting the following Exception from the View.

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: There is no ViewData item of type 'IEnumerable' that has the key 'Profile_Id'.

From View

@Html.DropDownList("Profile_Id", String.Empty)

Controller

public ViewResult Index( string name, string Profile_Id, int? page)
{

    ...
    ViewBag.Profile_Id = new SelectList(db.PROFILE.Where(y => y.ID== myId ), "Profile_Id", "Name");

    return View(prof.ToPagedList(pageNumber, pageSize));
}

Upvotes: 0

Views: 1949

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

I suppose that you are not getting this error the first time this view is rendered from the GET /Index action but rather when you submit the corresponding form to your POST action which renders the same view.

To fix this error make sure that inside your POST action you also populate the Profile_Id property like you did in the GET action if you intend to render the same view because it depends on it:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    ... process the data

    // populate the Profile_Id property because we are
    // redisplaying the same view which depends on it in order
    // to show the corresponding dropdown
    ViewBag.Profile_Id = new SelectList(db.PROFILE.Where(y => y.ID== myId ), "Profile_Id", "Name");

    return View(model);
}

Upvotes: 2

Related Questions