티스토리 뷰

💼 정보 ver1.0

Html.DropDownList

James Wetzel 2013. 5. 16. 19:27
728x90
반응형

public class Address

    {

        public string RegionID { get; set; }

        public string RegionName { get; set; }

    }


DropDownList 생성

<%: Html.DropDownList("name", new[] { new SelectListItem{ Text="text", Value="value"} }, "초기 출력 값") %>


DropDownList DB연동

public List<Address> SiList()

        {

            SqlConnection conn = new SqlConnection(this.connectionString);

            conn.Open();


            SqlCommand comm = new SqlCommand();

            comm.Connection = conn;

            comm.CommandType = System.Data.CommandType.StoredProcedure;

            comm.CommandText = "Address_Select_Si";

                        

            SqlDataReader reader = comm.ExecuteReader();


            List<Address> items = new List<Address>();


            while(reader.Read())

            {

                items.Add(new Address{ RegionID = reader["do"].ToString(), RegionName = reader["do"].ToString()  });

            }


            reader.Close();

            conn.Close();


            return items;

        }

<%: Html.DropDownList("si", (SelectList)ViewData["address_si"], new { @onchange = "siInitial();" })%>


3개의 DropDownList 컨트롤 하기


Control(앞에 선택한 값을 기준으로 뒤에 있는 항목들의 최상위 항목을 자동으로 보여주기 위한 것이다.)

 [HttpGet]

        public PartialViewResult CurrentWeather()

        {

            string initialSi = "서울특별시";

            string initialGu = "송파구";

            string initialDong = "가락본동";


            this.address = new AddressInheritance(new AddressImplement(this.connectionString));

            ViewData["address_si"] = new SelectList(this.address.SiList(), "RegionID", "RegionName", initialSi);

            ViewData["address_gu"] = new SelectList(this.address.GuList(initialSi), "RegionID", "RegionName", initialGu);

            ViewData["address_dong"] = new SelectList(this.address.DongList(initialSi, initialGu), "RegionID", "RegionName", initialDong);


            CurrentWeather currentWeather = GetCurrentWeather(this.x, this.y);


            return PartialView("CurrentWeather", currentWeather);

        }


 [HttpPost]

        public PartialViewResult CurrentWeather(string si, string gu, string dong)

        {

            CurrentWeather currentWeather = null;

            this.address = new AddressInheritance(new AddressImplement(this.connectionString));


            SelectList siSelectList = new SelectList(this.address.SiList(), "RegionID", "RegionName", si);

            ViewData["address_si"] = siSelectList;


            ArrayList guArrayList = null;

            SelectList guSelectList = new SelectList(this.address.GuList(si), "RegionID", "RegionName", gu);

            ViewData["address_gu"] = guSelectList;

                        

            SelectList dongSelectList = null;

            if (guSelectList.SelectedValue != null)

            {

                dongSelectList = new SelectList(this.address.DongList(si, guSelectList.SelectedValue.ToString()), "RegionID", "RegionName", dong);

            }

            else

            {

                guArrayList = new ArrayList();

                foreach(Address item in guSelectList.Items)

                {

                    guArrayList.Add(item.RegionID);

                }

                dongSelectList = new SelectList(this.address.DongList(si, guArrayList[0].ToString()), "RegionID", "RegionName", dong);

            }

            ViewData["address_dong"] = dongSelectList;


            if (guSelectList.SelectedValue == null || dongSelectList.SelectedValue == null)

            {

                ArrayList dongArrayList = new ArrayList();

                foreach (Address item in dongSelectList.Items)

                {

                    dongArrayList.Add(item.RegionID);

                }


                string guInitial = guSelectList.SelectedValue == null ? guArrayList[0].ToString() : guSelectList.SelectedValue.ToString();


                Coordinate xy = this.address.Getxy(siSelectList.SelectedValue.ToString(), guInitial, dongArrayList[0].ToString());

                currentWeather = GetCurrentWeather(xy.X, xy.Y);

            }


            if (siSelectList.SelectedValue != null && guSelectList.SelectedValue != null && dongSelectList.SelectedValue != null)

            {

                Coordinate xy = this.address.Getxy(siSelectList.SelectedValue.ToString(), guSelectList.SelectedValue.ToString(), dongSelectList.SelectedValue.ToString());

                currentWeather = GetCurrentWeather(xy.X, xy.Y);

            }


            return PartialView("CurrentWeather", currentWeather);

        }

View

<form id="addressForm" action="<%: Url.Action("CurrentWeather") %>" method="post">

         <%: Html.DropDownList("si", (SelectList)ViewData["address_si"], new { @onchange = "siInitial();" })%>

                        &nbsp;

         <%: Html.DropDownList("gu", (SelectList)ViewData["address_gu"],new { @onchange = "guInitial();" })%>

                        &nbsp;

         <%: Html.DropDownList("dong", (SelectList)ViewData["address_dong"], new { @onchange = "document.getElementById('addressForm').submit();" })%>

                        </form>


728x90
반응형