没有带有IEnumerable <selectlistitem> </selectlistitem>类型的键'taskTypes'的ViewData项

时间:2009-06-18 14:21:54

标签: c# asp.net-mvc drop-down-menu

在向ASP.NET MVC中的Html.DropDownList添加onchange属性时,我收到上面的错误消息:

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

当视图最初加载时,我没有收到错误。仅在更改所选项目后回发。我的控制器代码是:

[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();
    }

有谁知道问题可能是什么?

由于

1 个答案:

答案 0 :(得分:3)

该控制器方法的AcceptVerbs属性表明它将构建该ViewData实例并返回关联的View以显示您的表单。您确定负责处理表单提交(或保存数据)的控制器方法是否以相同的方式构建ViewData实例?