或许简单,但我似乎失去了一些东西。
两种型号:
public class Hardware
{
[Required]
public int Id { get; set; }
public int SerialNum { get; set; }
public int ProductNum { get; set; }
public string Notes { get; set; }
public DateTime PurchaseDate { get; set; }
public DateTime WarrantyExpiration { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
}
public class Manufacturer
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Hardware> Hardware { get; set; }
}
当我去到硬件上创建的看法,我希望能够从制造商的下拉列表中选择,并在其提交它应该建立的硬件和选择制造商之间的关系。
目前,我已经使用以下建立在控制器的SelectList了
SelectList selectList = new SelectList(db.Manufacturers, "Id", "Name");
ViewBag.selectList = selectList;
然后铸造它的观点:
@Html.DropDownListFor(model => model.Manufacturer, ViewBag.selectList as SelectList)\
但是,好像应该有更好的方式来做到这一点 - 也许创造,从硬件继承了的SelectList类型属性一个ViewModel?