0

我在处理强类型列表框中的多个值时遇到问题。我有一个可以有多个技术类的事件类。

这是我的简化事件类:

public class Event
{
    public int ID { get; set; }
    public IEnumerable<Technology> Technologies { get; set; }
}

我正在使用这个

public List<Technology> Technologies { get; set; }

并改为

public IEnumerable<Technology> Technologies { get; set; }

但仍然有同样的错误。

这是技术课,非常简单的一课

public class Technology
{
    public int ID { get; set; }
    public String Description{ get; set; }
}

这是我的简化控制器

public ActionResult Create()
    {
        var TechnologyQry = from d in db.Technology
                              orderby d.Description
                              select new { d.ID, d.Description };
        ViewBag.eventTechnology = new MultiSelectList(TechnologyQry ,"ID","Description");
        return View();
    }

这是呈现 ListBox 的视图部分

@Html.ListBoxFor(model => model.Technologies, (MultiSelectList)ViewBag.eventTechnology)

这就是我得到的错误

具有键 'Technologies' 的 ViewData 项的类型为 'System.Collections.Generic.List`1[[stuff.Models.Technology, stuff, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 但是必须是“IEnumerable”类型。/

对不起,英语不是我的主要语言。我会很感激我能得到的任何帮助。谢谢你。

4

1 回答 1

0

您在 View 中定义了什么模型@model ...
除此之外, Create 方法没有返回用于/作为模型的东西。如果您希望列表框在名称属性中称为“技术”,则可以改用:

@Html.ListBox("Technologies", (MultiSelectList)ViewBag.eventTechnology)
于 2011-09-02T22:07:44.177 回答