不能键入'System.Web.Mvc.SelectList'隐式转换为“了System.Collections.Generic.ICollection< System.Web.Mvc.SelectList>' [英] Cannot implicitly convert type 'System.Web.Mvc.SelectList' to 'System.Collections.Generic.ICollection<System.Web.Mvc.SelectList>'

查看:655
本文介绍了不能键入'System.Web.Mvc.SelectList'隐式转换为“了System.Collections.Generic.ICollection< System.Web.Mvc.SelectList>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一定值添加到我的SelectList数据成员在我的对象,但我得到一个错误

I am trying to add some values into my SelectList data member in my object but I get an error

public ActionResult Create()
    {

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

        return View(paf);
    }

这是我的科瑞功能和paf.Sizes / paf.Suppliers code不起作用。

that is my creat function, and the paf.Sizes / paf.Suppliers code does not work.

我productaddform类:

My productaddform class:

    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

And my methods in my 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" };
    }

哪些错误?

推荐答案

供应商的SelectList 的集合。所以,你需要项目添加到集合

Suppliers is a collection of SelectList. So you need to Add item into the collection

修改

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

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

这篇关于不能键入'System.Web.Mvc.SelectList'隐式转换为“了System.Collections.Generic.ICollection&LT; System.Web.Mvc.SelectList&GT;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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