tags:

views:

34

answers:

2

Hello

I have a problem with posting a multiselect, I get error: "There is no ViewData item with the key 'NotificationUsergroups' of type 'IEnumerable'."

In the controller I have:

            MultiSelectList NotificationUsergroups = new MultiSelectList(Usergroups, "UsergroupID", "UsergroupName", selectedNotificationUsergroupIDs);

        ViewData["NotificationUsergroups"] = NotificationUsergroups;

In the view I have:

<%= Html.ListBox("NotificationUsergroups")%>

And in the post action:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ObjectEdit([BindAttribute(Include = "BookingObjectID,BookingObjectName,Activated,ActivationStartDate,ActivationEndDate,AvalibleObjects,AvalibleObjectsPerBooking")]BookingObject bookingobject, int[] Objectcategories, int[] NotificationUsergroups, int[] CancellationUsergroups)
    {
        if (ModelState.IsValid)
        {
            try
            {
                _bs.SaveBookingObject(bookingobject);

                if (NotificationUsergroups != null)
                    _bs.SaveNotificationUsergroups(bookingobject.BookingObjectID, NotificationUsergroups);

                return View("CreateObject", new BookingObjectsAdminEditViewModel { BookingObject = bookingobject });
            }
            catch {
                ModelState.AddModelError("SomeError", "errrrrrrrrror");
            }
        }

What might be wrong? I've checked the spelling and all, works if I dont run with the multiselect listing.

What makes the data "disappear"?

Thanks in advance /M

+1  A: 

You will need to set the viewdata in the POST accepting method as well because if it has an error it will return to the original view.

Kindness,

Dan

Or include them in your model :) You would need to alter view tho.

Daniel Elliott
what type shall it be if I have it in model?
molgan
It would be a multiselect list also,in your view it would be something like: <%= Html.ListBox("NotificationUsergroups", Model.NotificationUsergroups)%>
Daniel Elliott
what type shall I use, get type missmatch errors
molgan
A: 

Your action must setup your ViewData.

If your action calls

 ViewData["NotificationUsergroups"] = NotificationUsergroups;

everything should be alright.

Arnis L.