0

I have a list created from a controller which is passed to a viewbag item:

        List<SelectListItem> PTL = new List<SelectListItem>();


        List<PT> PTL2 = db.PT.ToList();



        foreach (var item in PTL2)
        {
            PTL.Add(new SelectListItem { Value = item.ID.ToString(), Text = item.Name });
        }

        ViewBag.PTL2 = PTL2;

Then, in the view, I tried the following from another question here:

               @Html.DropDownList("test", new SelectListItem[]{ 
            new SelectListItem() {Text = "Exemplo1", Value="Exemplo1"},
            new SelectListItem() {Text = "Exemplo2", Value="Exemplo2"},
            new SelectListItem() {Text = "Exemplo3", Value="Exemplo3"}}
            , htmlAttributes: new { @class = "form-control" })

which worked fine, but, if I try to edit it to the following:

               @Html.DropDownList("test", ViewBag.PTL2, htmlAttributes: new { @class = "form-control" })

I get various errors.

I have spent hours on this trying different combinations, different castings and more, but, I just don't seem to be making progress.

I have seen so many different errors - the most common one being '

There is no ViewData item of type IEnumerable<SelectListItem> that has the key “test”'

However, I tried casting and changing the name as per different questions here, but, I just can't seem to get past this.

At this point, I am thinking of just making a manual HTML Drop Down list and a foreach loop for the content, but, this seems a waste.

Does anyone know what I have done wrong?

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
wil
  • 2,019
  • 2
  • 15
  • 14
  • 1
    You need to cast it - `@Html.DropDownList("test", (IEnumerable)ViewBag.PTL2, ...)` Recommend you also read [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) and do not use `ViewBag` –  Sep 12 '16 at 05:53
  • Thanks, I fear I am too far along to change to not use ViewBag if it can work quick (just a small internal tool), but I will read and learn for next time. – wil Sep 12 '16 at 05:58

2 Answers2

4

The second parameter must be a collection of System.Web.Mvc.SelectListItem objects that are used to populate the drop-down list. But, you are using ViewBag property in this case and the type of this is dynamic. So, you must cast it to the collection of the SelectListItem. Otherwise, you will get this error in MVC 4+:

Extension methods cannot be dynamically dispatched

So. as a result just change ViewBag.PTL2 to ViewBag.PTL2 as IEnumerable<SelectListItem>.

Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
0

i hope it is not too late ,this is how i do it with .net core in your controller ,use below code

 public ActionResult ShowCalendar()
        {
            var programs = new Microsoft.AspNetCore.Mvc.Rendering.SelectList((new Programs().GetAll().ToList(), "ID", "Name")  
 
            ViewBag.AllItems = programs;
 
            return View();
        }

and in your cshtml file use the below

            <select id="ItemID" class="form-control"
                    asp-items="ViewBag.AllItems">
                <option disabled selected>--- @Localizer["PleaseSelect"] ---</option>
            </select>

will do the job for you

Ali
  • 1,080
  • 16
  • 22