2

I am having an issue with a dropdownlist I have in a PartialView.

Basically get the following error There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Status'.

Here is my ActionResult method inside the controller

[HttpGet]
public  ActionResult Status()
{
   List<SelectListItem> Status = new List<SelectListItem>();

   Status.Add(new SelectListItem { Text = "Open", Value = "1" });
   Status.Add(new SelectListItem { Text = "Closed", Value = "2" });
   Status.Add(new SelectListItem { Text = "Delete", Value = "3" });

   ViewData["Status"] = Status;

   return View();
}

My Status Partial View where I call the select list

@Html.DropDownList("Status", ViewData["Status"] as SelectList)

And then I call the partial view from my main view as follows

@Html.Partial("Status")

I am just not sure why its giving the above error. I took at look at this asp.net mvc dropdownlist no ViewData item and still not able to rectify the issue?

Update

As per @MikeDebela solution my Status Action Item was never been called. So used the following syntax to call the Action directly

@{Html.RenderAction("Status");}
Community
  • 1
  • 1
KJSR
  • 1,679
  • 6
  • 28
  • 51
  • Your code totally works fine when copied and pasted in a local MVC5 project – Shyju Oct 11 '16 at 13:46
  • 1
    You pass a `List` into view data and try to cast it to `SelectList` that cast wont work – Nkosi Oct 11 '16 at 13:48
  • @Shyju, I don't think so. You can create a `SelectList` from a `List` but only if there is some TypeConverter I don't casting would work. Do you have some reference material I can look at to verify this? – Nkosi Oct 11 '16 at 13:58
  • 1
    @Nkosi. Check this fiddle which does that https://dotnetfiddle.net/KRxJhN – Shyju Oct 11 '16 at 14:00
  • Ok, I'll take a look. thanks @Shyju. – Nkosi Oct 11 '16 at 14:02
  • Hi Shyju I am not sure why mine isn't working? However does it matter if I am using Partial views? Because as mentioned above I am calling a partial view inside my main view. – KJSR Oct 11 '16 at 14:03
  • 2
    The reason is your `Status` action will never called. Use `@{Html.RenderAction("Status");}` instead – Mike Debela Oct 11 '16 at 14:28
  • Thanks @MikeDebela for the solution. It works grand now :) – KJSR Oct 11 '16 at 14:31
  • 1
    @MikeDebela you should add that as an answer so that the OP can accept it. – Nkosi Oct 11 '16 at 14:49

3 Answers3

3

When you use Partial helper, the runtime looks for the view and renders a string (your action will never get executed). On the other hand, RenderAction executes the action and displays the result. So, use:

@{Html.RenderAction("Status");}
Misgevolution
  • 825
  • 10
  • 22
Mike Debela
  • 1,930
  • 3
  • 18
  • 32
0
@Html.DropDownList("Status", new SelectList((System.Collections.IEnumerable) ViewData["Status"],"Text","Value"))

you can use this line to fill dropdownlist with ViewData

Ibrahim Alsurkhi
  • 113
  • 1
  • 13
  • There is something wrong with your answer. `IEnumerable` requires 1 type arguments – KJSR Oct 11 '16 at 13:44
  • try this @Html.DropDownList("Status", new SelectList((System.Collections.IEnumerable) ViewData["Status"],"Text","Value")) – Ibrahim Alsurkhi Oct 11 '16 at 13:47
  • Thanks Ibrahim for the correction however it is now giving me a new error `Value cannot be null.` – KJSR Oct 11 '16 at 13:49
0

Please check that the code as follows because it is working with me

public ActionResult Index()
    {
        List<SelectListItem> Status = new List<SelectListItem>();

        Status.Add(new SelectListItem { Text = "Open", Value = "1" });
        Status.Add(new SelectListItem { Text = "Closed", Value = "2" });
        Status.Add(new SelectListItem { Text = "Delete", Value = "3" });

        ViewData["Status"] = Status;
        return View();
    }


@Html.DropDownList("Status", new SelectList((System.Collections.IEnumerable)ViewData["Status"], "Value", "Text"))

or you can use

@Html.DropDownList("Status", (IEnumerable<SelectListItem>)ViewData["Status"])
Ibrahim Alsurkhi
  • 113
  • 1
  • 13