动态展示城市下拉框
<div>
<% var items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "北京", Value = "1" });
items.Add(new SelectListItem() { Text = "上海", Value = "2",Selected=true});//选中项
items.Add(new SelectListItem() { Text = "天津", Value = "3" });
items.Add(new SelectListItem() { Text = "重庆", Value = "4" });
items.Add(new SelectListItem() { Text = "广州", Value = "5" });
%>
<%=Html.DropDownList("area",items,"请选择") %>
<%=Html.DropDownList("area",(SelectList)ViewBag.Cities,"请选择") %>
</div>
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public string desc { get; set; }
public City(int id,string name)
{
this.Id =id;
this.Name=name;
}
}
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<City> cities = GetCities();
SelectList list = new SelectList(cities, "Id", "Name", cities[1].Id);
ViewBag.Cities = list;
return View();
}
List<City> GetCities()
{
List<City> cities = new List<City>();
cities.Add(new City(1, "北京"));
cities.Add(new City(2, "上海"));
cities.Add(new City(3, "天津"));
cities.Add(new City(4, "重庆2"));
cities.Add(new City(5, "广州"));
return cities;
}