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