没有与IEnumerable类型&LT的关键“taskTypes”无ViewData的项目; SelectListItem> [英] There is no ViewData item with the key 'taskTypes' of type IEnumerable<SelectListItem>

查看:139
本文介绍了没有与IEnumerable类型&LT的关键“taskTypes”无ViewData的项目; SelectListItem>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到添加onchange属性在ASP.NET MVC一个Html.DropDownList时,上述错误消息:

I'm getting the error message above when adding an onchange attribute to a Html.DropDownList in ASP.NET MVC:

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

在视图开始加载,我没有得到这个错误。只有回发时,选择的项目被改变之后。我的控制器code是:

When the view initially loads, I do not get the error. Only when posting back after the selected item is changed. My controller code is:

[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 might be?

感谢

推荐答案

该AcceptVerbs在该控制器的方法属性表明,它会建立了ViewData的实例,并返回相应的视图来显示您的窗体。你可以肯定,负责处理表单提交(或保存数据)控制器方法,建立了以同样的方式ViewData的实例吗?

The AcceptVerbs attribute on that controller method indicates that it will build up that ViewData instance and return the associated View to display your form. Are you certain that the controller method responsible for handling the form submission (or saving the data) is building up that ViewData instance in the same manner?

这篇关于没有与IEnumerable类型&LT的关键“taskTypes”无ViewData的项目; SelectListItem&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆