我正在尝试在我的对象中的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" };
}
怎么了?
答案 0 :(得分:4)
Suppliers
是SelectList
的集合。所以你需要将项目添加到集合
更改
paf.Suppliers = new SelectList(m.GetAllSuppliersList(), "Id", "Name");
到
paf.Suppliers.Add(new SelectList(m.GetAllSuppliersList(), "Id", "Name"));