I get the error message above when adding the onchange a...">

There is no ViewData element with key "taskTypes" of type IEnumerable <SelectListItem>

I get the error message above when adding the onchange attribute to the Html.DropDownList in ASP.NET MVC:

<td><%= Html.DropDownList("taskTypes", (IEnumerable<SelectListItem>)ViewData["TaskTypes"], "None", new { onchange = "document.getElementById('NewTask').submit()" })%></td>

When a view is initially loaded, I don't get an error. Only after publication after the selected item. My controller code:

[AcceptVerbs(HttpVerbs.Get), RequiresAuthentication]
    public ActionResult NewTask()
    {
        List<SelectListItem> dropDownData = new List<SelectListItem>();
        List<SelectListItem> statusDropDownData = new List<SelectListItem>();

        foreach (TaskStatus t in tasks.GetTaskStatus())
        {
            statusDropDownData.Add(new SelectListItem { Text = t.Status, Value = t.TaskStatusID.ToString() });
        }

        foreach (TaskType t in tasks.GetTaskTypes())
        {
            dropDownData.Add(new SelectListItem { Text = t.Reference, Value = t.TaskTypeID.ToString() });
        }

        ViewData["TaskStatus"] = statusDropDownData;
        ViewData["TaskTypes"] = dropDownData;

        if (Request["taskTypes"] != null)
        {
            string tt = Request["taskTypes"];
        }


        return View();
    }

Does anyone know what the problem is?

thanks

+3
source share
1 answer

AcceptVerbs , ViewData , . , , ( ), ViewData ?

+3

Source: https://habr.com/ru/post/1710771/


All Articles