0

I have a Controller and a View and I would like to populate a dropdownlist.

My Controller looks like this:

namespace AAA.Controllers
{
    public class BBBController: Controller
    {
        [HttpPost]
        public ActionResult Sort_Order()
        {
            List<SelectListItem> liSort = new List<SelectListItem>();
            liSort.Add(new SelectListItem() { Text = "--Select--", Value = "0", Selected = true });
            liSort.Add(new SelectListItem() { Text = "ASC", Value = "1" });
            liSort.Add(new SelectListItem() { Text = "DESC", Value = "2" });

            ViewData["ddSort"] = liSort;
            return View();
        }
    }
}

My View looks like this:

@model IEnumerable<AAA.Models.xyz>

@{
    ViewBag.Title = "List";
}

<br />
<h2>List</h2>

<table class="table">
    <tr>
        <th>
            Sort order
        </th>
    </tr>
    <tr>
        <td>
            @using (Html.BeginForm("Sort_Order", "BBB", FormMethod.Post))
            {
               @Html.DropDownList("Sort", ViewData["ddSort"] as IEnumerable<SelectListItem>, new { @class = "form-control", @style = "width:200px; height:30px" })
            }
        </td>
            </tr>
</table>

When I run this I get a error:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Sort'.*

Can someone help me and tell me what I am doing wrong?

Grega
  • 11
  • 4
  • Possible duplicate of [The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable'](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Aug 27 '18 at 10:52
  • Have you also populated `ViewData["ddSort"]` in the GET method? –  Aug 27 '18 at 10:54
  • I am fairly new at this, so can you explain what you ment by "Have you also populated ViewData["ddSort"] in the GET method?" – Grega Aug 27 '18 at 11:07
  • All you have shown is the `[HttpPost]` method - you need the same code in your `[HttpGet]` method (i.e. to set `ViewData["ddSort"] = liSort;`) –  Aug 27 '18 at 11:11
  • OK I don't get it. I did what you said (I hope) and it still doesn't work. Is it possible for you to write the code so I can compare? Thank you. – Grega Aug 27 '18 at 12:40
  • To do this correctly, have a look at the code in the duplicate - the error means that `ViewData["ddSort"]` is `null` (and is that really the code for the POST method or is it really the code for the GET method?) –  Aug 27 '18 at 12:48

1 Answers1

0

You need to initialize ViewData["ddSort"] in the GET method. Then you can use it in your view.

    [HttpGet]
    public ActionResult Sort_Order()
    {
        List<SelectListItem> liSort = new List<SelectListItem>();
        liSort.Add(new SelectListItem() { Text = "--Select--", Value = "0", Selected = true });
        liSort.Add(new SelectListItem() { Text = "ASC", Value = "1" });
        liSort.Add(new SelectListItem() { Text = "DESC", Value = "2" });

        ViewData["ddSort"] = liSort;
        return View();
    }
Rakesh Kumar
  • 2,701
  • 9
  • 38
  • 66