MVC Dropdownlistfor<>

后端 未结 4 663
轮回少年
轮回少年 2021-02-10 14:41

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())

提交回复
热议问题