表中的ASP.NET MVC填充下拉列表

时间:2019-11-04 14:10:52

标签: asp.net-mvc linq

我从CODE表中得到了一个列名,例如Projects_Table。如何使用linq选择所有这些列值。 我尝试过:

public ToolEntities db = new ToolEntities();

// GET: Projects_Table
public ActionResult Index()
{
    ViewBag.list = new SelectList(db.Projects_Table, "CODE", "CODE");
    return View(db.Projects_Table.ToList());
}

我想像从.cshtml页面中的下拉菜单一样填充此Viewbag.list。Linq不返回数据。

1 个答案:

答案 0 :(得分:1)

您可以像这样填充# never used Enumerator::Generator directly, but you called it out specifically # in your question, and this seems to be doing the trick to get it working enum = Enumerator::Generator.new do |y| y.yield "ant" y.yield "bear" y.yield "cat" end p enum.reduce(&:+) # output: "antbearcat" # crude example of modifying the strings as you join them p enum.reduce('') { |memo, word| memo += word.upcase + ' ' } # output: "ANT BEAR CAT " 而不是 String id = exchange.getRequest().getId(); exchange.getResponse().getHeaders().add("id",id);

SelectListItem

您的视图SelectList这样

List<SelectListItem> projectTableList = new List<SelectListItem>();
foreach (var projectTable in db.Projects_Table)
{
    projectTableList.Add(new SelectListItem
    {
       Text=projectTable.Code,
       Value=projectTable.ID.ToString()
     });
}
ViewBag.list = projectTableList;
return View();