我不明白如何使用我在这里看到的示例来制作给定我的模型的下拉列表。我正在创建一个用于添加新项目的页面。我的页面的模型是定义我尝试添加的项目的类,而不是下拉列表的列表(我找到的示例使用)。所以我的页面模型是:
@model 主页管理器.域.库存项
库存项的位置
public class InventoryItem
{
public virtual int Id { get; set; }
public virtual DateTime Added { get; set; }
[Required]
[StringLength(50, ErrorMessage="Item name cannot exceed 50 characters.")]
public virtual string Name { get; set; }
public virtual string Size { get; set; }
public virtual Location Location { get; set; }
public virtual string Instructions { get; set; }
public virtual int Count { get; set; }
public virtual string UPC { get; set; }
public InventoryItem()
{
// default non null dattetimevalues
Added = DateTime.Now;
}
}
我最终要保存的项目将具有该位置的 ID。我在这里看到的所有示例都有查找(在我的例子中是一个简单的 ID 名称对的位置)作为模型,如果我的页面的其余部分用于该项目,该模型将无法工作。
我知道这是个菜鸟。我很感激你的帮助。
S
在模型上包含一个具有Location
所有可能性的 IEnumerable<Location>
属性。 然后,像这样设置您的DropDownList
:
@Html.DropDownListFor(m => m.Location.LocationId, new SelectList(Model.Locations, "LocationId", "LocationName"), "Select Location...", new {id = "whatever"})
我不知道你Location
类型是什么样子的,所以我只是推断出名字。
有关详细信息,请参阅 SelectList
构造函数文档。