无法将类型'System.Web.Mvc.SelectList'隐式转换为'System.Collections.Generic.ICollection <system.web.mvc.selectlist>'</system.web.mvc.selectlist>

时间:2014-02-06 21:17:05

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在尝试在我的对象中的SelectList数据成员中添加一些值,但是我收到错误

public ActionResult Create()
    {

        var paf = new ProductAddForm();
        paf.Sizes = new SelectList(m.GetProductSizes());
        paf.Suppliers = new SelectList(m.GetAllSuppliersList(), "Id", "Name");

        return View(paf);
    }

这是我的creat函数,而paf.Sizes / paf.Suppliers代码不起作用。

我的productaddform类:

    public class ProductAddForm
{

    public double MSRP { get; set; }

    public string Name { get; set; }

    public string ProductId { get; set; }

    public ICollection<SelectList> Sizes { get; set; }

    public ICollection<SelectList> Suppliers { get; set; }

    public string UPC { get; set; }


}

我在manager.cs中的方法

public IEnumerable<SupplierList> GetAllSuppliersList()
    {

        var fetchedObjects = ds.Suppliers.OrderBy(n => n.Name);

        var Suppliers = new List<SupplierList>();

        foreach (var item in fetchedObjects)
        {

            var s = new SupplierList();
            s.Name = item.Name;
            s.Id = item.Id;

            Suppliers.Add(s);
        }

        return (Suppliers);

    }

    public List<string> GetProductSizes()
    {

        return new List<string>() { "Small", "Medium", "Large" };
    }

怎么了?

1 个答案:

答案 0 :(得分:4)

SuppliersSelectList的集合。所以你需要将项目添加到集合

更改

paf.Suppliers = new SelectList(m.GetAllSuppliersList(), "Id", "Name");

paf.Suppliers.Add(new SelectList(m.GetAllSuppliersList(), "Id", "Name"));
相关问题