Article Post

How to bind the Dropdwon staticlly and Dynnamiclly in C# back end.

How to bind the Dropdown staticlly and Dynnamiclly in C# back end.

In this Article, i would like to show how to bind dropdown static and dynamic  from back end.

For Static:

private void BindDropDowns()
        {
            List<CommonModel> list = new List<CommonModel>();
            var obj = new CommonModel();
            //obj.Id = 1;
            //obj.Title = "Option 1";
            //list.Add(obj);
            obj = new CommonModel();
            obj.Id = 2;
            obj.Title = "Option 2";
            list.Add(obj);
            obj = new CommonModel();
            obj.Id = 3;
            obj.Title = "Option 3";
            list.Add(obj);
            obj = new CommonModel();
            obj.Id = 4;
            obj.Title = "Option  4";
            list.Add(obj);
            obj = new CommonModel();
            obj.Id = 5;
            obj.Title = "Option 5";
            list.Add(obj);
            obj = new CommonModel();
            obj.Id = 6;
            obj.Title = "Covid-19 Sitreps";
            list.Add(obj);
            obj = new CommonModel();
            obj.Id = 7;
            obj.Title = "Other Publications";
            list.Add(obj);
            //obj = new CommonModel();
            //obj.Id = 8;
            //obj.Title = "Videos";
            //list.Add(obj);


            ViewBag.TypeName = new SelectList(list, "Id", "Title");
        }

 

For Dynamic:

private void BindDropDown()
        {
            var cntry = _storedb.Countries.ToList();
            ViewBag.CountryLsit = new SelectList(cntry, "CountryID", "Name");

            var city = _storedb.Cities.ToList();
            ViewBag.CityList = new SelectList(city, "Id", "title");

            var ent_puprose = _storedb.EntityPuproseIds.ToList();
            ViewBag.ent_puproseList = new SelectList(ent_puprose, "Id", "title");


        }

By     08-Sep-2022 Views  7

Solutions and sugestions