MVC Dropdownlistfor<>

后端 未结 4 775
小鲜肉
小鲜肉 2021-02-10 14:37

I just started a project in MVC. I am new to MVC asp.net. I want to add a dropdown list box like I used to add in asp.net.

code glimpses in asp.net for Dropdownlist box

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-10 15:14

    You can also follow another approach which is to collect all dropdown lists in 1 helper class and return theses lists each with a static function.

    Ex. Dropdown helper class

    namespace Asko.Web.Mvc.Infrastructure.Utils
    {
        public static class DropdownHelper
        {
            public static IEnumerable GetAllCategories()
            {
                var categories = category.GetAll();
                return categories.Select(x => new SelectListItem { Text = x.categoryName, Value = x.categoryId.ToString()}));
            }
        }
    }
    

    And here you are going to use it in the page:

    
          Category: 
          
    @Html.DropDownListFor(m => m.CategoryId, DropdownHelper.GetAllCategories())

提交回复
热议问题