首页 > 代码库 > asp.net mvc 添加下拉列表

asp.net mvc 添加下拉列表

新手刚学mvc

controller中:

 public ActionResult Index(string namelist)
{
            var list = new List<string>();

            var listQry = from d in db.Movies
                          orderby d.name
                          select d.name;
            list.AddRange(listQry.Distinct());
            ViewBag.namelist = new SelectList(list);

            var movies = from m in db.Movies
                         select m;

            if (!String.IsNullOrEmpty(namelist))
            {
                movies = movies.Where(x => x.name == namelist);
            }
            return View(movies);
            
}

html中:

@using (Html.BeginForm("Index", "Movies", FormMethod.Get)) { 
        <p>
            下拉列表:@Html.DropDownList("namelist", "All")
            <input type="submit" value=http://www.mamicode.com/"查询"><br/>
        </p>
}

All是下拉列表显示的默认值。

asp.net mvc 添加下拉列表