Populating ASP.NET MVC DropDownList

前端 未结 7 1167
孤街浪徒
孤街浪徒 2020-12-08 00:42

OK, I\'ve been Googling for hours and trying everything and can\'t get anything to work. I am learning MVC using Sharp Architecture and have generated some basic forms for c

7条回答
  •  误落风尘
    2020-12-08 00:54

    This is View - MVC Controller

    @Html.DropDownListFor(m => m.Entity, new ABC.Models.DropDownPopulate().MyMethod, new { @class = "form-control input-inline input-medium" })
    

    MyMethod Get Data List Bind With Dropdown Using SelectListItems

     public List MyMethod
            {
                get
                {
                    List dropdownList = new List();
                    var items = new DropDown_Bl().GetAll();
                    foreach (var item in items)
                    {
                        SelectListItem dropdownItem = new SelectListItem();
                        dropdownItem.Value = item.RnID.ToString();
                        dropdownItem.Text = item.Description;
                        dropdownList.Add(dropdownItem);
                    }
                    dropdownList.Insert(0, new SelectListItem() { Text = "Please Select", Value = "", Selected = true });
                    return dropdownList;
                }
            }
    

    GetAll Method In Dropdown_Bl - Used to get data as list from database

      public List GetAll()
            {
                var Items = _Context.MyRepository.GetMany(q => q.Area == "ASD").ToList();
                return Items ;
            }
    

提交回复
热议问题