zoukankan      html  css  js  c++  java
  • 动态展示城市下拉框

     <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;
            }




  • 相关阅读:
    VS2010导入DLL的总结
    [转]C#事件简单示例
    VS2010中实现TreeView和Panel的动态更新
    【JZOJ1282】打工
    【NOIP2016提高A组五校联考2】tree
    【NOIP2016提高A组五校联考2】running
    【NOIP2016提高A组五校联考2】string
    8月~9月学习总结
    NOIP2016提高A组五校联考2总结
    NOIP2016提高A组五校联考1总结
  • 原文地址:https://www.cnblogs.com/bignine/p/13775845.html
Copyright © 2011-2022 走看看