我正在尝试将一些值添加到对象的 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);
}
这是我的创建功能,也是PAF。尺寸/帕夫。供应商代码不起作用。
我的产品添加表单类:
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; }
}
还有我在经理身上的方法.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" };
}
怎么了?
Suppliers
是SelectList
的集合。所以你需要将项目添加到集合中
改变
paf.Suppliers = new SelectList(m.GetAllSuppliersList(), "Id", "Name");
自
paf.Suppliers.Add(new SelectList(m.GetAllSuppliersList(), "Id", "Name"));