【问题标题】:DropDownListFor: no ViewData item of type 'IEnumerable<SelectListItem>' that has the keyDropDownListFor:没有具有键的“IEnumerable<SelectListItem>”类型的 ViewData 项
【发布时间】:2016-12-03 07:12:35
【问题描述】:

我刚刚在 Stack Overflow 上创建了我的帐户,所以这是我第一次提出问题:我的 DropDownList 有问题。我不知道我做错了什么,因为我对 ASP-NET 和 MVC 5 比较陌生,所以请原谅我做的任何可怕的事情

控制器:

public ActionResult typeDevices()
    {
        List<SelectListItem> typelist = new List<SelectListItem>();
        DropDownModel type = new DropDownModel();


        type.TypeDevicesId = 1;
        type.TypeDevicesValue = "typeDevice1";

        typelist.Add(new SelectListItem() { Value = type.TypeDevicesValue, Text = type.TypeDevicesId.ToString() });

        type.TypeDevicesId = 2;
        type.TypeDevicesValue = "typeDevice2";

        typelist.Add(new SelectListItem() { Value = type.TypeDevicesValue, Text = type.TypeDevicesId.ToString() });

        type.TypeDevicesId = 3;
        type.TypeDevicesValue = "typeDevice3";

        typelist.Add(new SelectListItem() { Value = type.TypeDevicesValue, Text = type.TypeDevicesId.ToString() });

        SelectList listvalues = new SelectList(typelist, "Text", "Value");
        ViewBag.DropDownValues = listvalues;
        ViewData["DropDownData"] = new SelectList(listvalues, "Text", "Value");
        return View();
    }

型号:

public class DropDownModel
{
    public int TypeDevicesId { get; set; }
    public string TypeDevicesValue { get; set; }
}

索引:

<p>
    <label class="field" for="Type">Type:</label>
    @Html.DropDownList("DropDownData", (SelectList)ViewBag.DropDownValues))
</p>

此代码来自我正在关注的视频。 Link to video

我得到的错误是

没有具有键“DropDownData”的“IEnumerable”类型的 ViewData 项

【问题讨论】:

  • 从第一个创建第二个相同的 SelectList 是毫无意义的额外开销 - 它只是 ViewBag.DropDownValue = typelist; 并且您不能将 &lt;select&gt; 绑定到 IEnumerable&lt;SelectListItem&gt; 所以 ViewData["DropDownData"] = new SelectList(listvalues, "Text", "Value"); 没有意义。如果您想选择第二个选项,它的ViewData["DropDownData"] = 2;。但是你的代码是糟糕的做法,我建议你阅读this question/answer
  • 感谢建设性的批评,我去看看:) 出于好奇,如果我想从数据库中取出一个项目,我应该怎么做?假设 type_devices 与表 computer_tabel 有关系。我希望它选择与计算机 ID 相关的正确的?
  • 我给你的链接显示了如何做到这一点:)
  • 好的,谢谢!

标签: asp.net-mvc razor


【解决方案1】:

改这行代码就行了

ViewBag.DropDownValues = listvalues;

ViewBag.DropDownValues = typelist;

您需要在下拉列表中提供IEnumerable&lt;SelectListItem&gt; 类型的数据。您的 listvalues 属于 SelectList 类型,但您的 typelist 属于必需类型。

【讨论】:

  • 谢谢,但它不起作用,我仍然收到错误:附加信息:没有具有键 'DropDownData' 的 'IEnumerable' 类型的 ViewData 项。可能与@Html.DropDownList("DropDownData") 有关吗,也许应该是DropDownValues?
【解决方案2】:

试试这个:

ViewData["DropDownData"] = typelist;
@Html.DropDownList("DropDownData", (List<SelectListItem>)ViewData["DropDownData"])

【讨论】:

  • 谢谢,但不幸的是,这也不起作用。我开始认为 DropDownLists 只是不喜欢我:P
  • ViewData["DropDownData"] = typelist;
  • ViewData 名称和下拉列表名称必须相同。如下@Html.DropDownList("DropDownData")
  • 我试过了,但我会再试一次。它仍然给我同样的错误:prntscr.com/dehf7x
  • 是的,ViewData 名称和下拉列表名称是一样的。
【解决方案3】:

问题已解决,我创建了一个公共 actionResult TypeDevices 但我从未调用过它。所以这有点愚蠢。感谢大家的帮助! :)

我通过将 Public ActionResult TypeDevices 更改为 Public void Type Devices 并在 ActionResult 索引中调用 TypeDevices(); 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2020-06-28
    • 2020-08-20
    • 2011-02-20
    • 2015-03-11
    相关资源
    最近更新 更多