我写这段代码来提取要添加到列表中的特定列,但是它是错误的。我要:
List<Stone_Names> Stones = new List<Stone_Names>();
using (PSPlatformEntities1 p = new PSPlatformEntities1())
{
Stones.Add(new SelectListItem
{
Text = p.Stone_Names["stoneName"].ToString(),
Value = p.Stone_Names["nameID"]
});
}
答案 0 :(得分:0)
非常简单,首先从数据库获取列表,然后将绑定绑定到下拉列表
ViewBag.DDlist= db.Stones.ToList().Select(x => new
SelectListItem()
{
Text = x.stoneName,
Value = x.nameID.ToString()
});
可见
@Html.DropDownListFor(model => model.modelProperty,
new SelectList(ViewBag.DDlist, "Value", "Text"),
"Select value", new { @class = "form-control" })
注意:stoneName,nameID应该是您的表名