Design

Design
asp.net mvc

2016年5月23日 星期一

後端資料庫產生Dropdownlist asp.net mvc

public ViewResult Index()
        {
            //Create db context object here 
            AdventureWorksDbContext dbContext = new AdventureWorksDbContext();
            //Get the value from database and then set it to ViewBag to pass it View
            IEnumerable items = dbContext.Employees.Select(c => new SelectListItem
                  {
                      Value = c.JobTitle,
                      Text = c.JobTitle
 
                  });
            ViewBag.JobTitle = items;
            return View();
        }
@Html.DropDownList("JobTitle", "Select a Value")

沒有留言:

張貼留言