validate a dropdownlist in asp.net mvc

前端 未结 4 1321
遇见更好的自我
遇见更好的自我 2020-12-01 01:12
//in controller
ViewBag.Categories = categoryRepository.GetAllCategories().ToList();

//in view
 @Html.DropDownList(\"Cat\", new SelectList(ViewBag.Categories,\"ID\"         


        
4条回答
  •  我在风中等你
    2020-12-01 01:21

    There is an overload with 3 arguments. Html.DropdownList(name, selectList, optionLabel) Update: there was a typo in the below code snippet.

    @Html.DropDownList("Cat", new SelectList(ViewBag.Categories,"ID", "CategoryName"), "-Select Category-")
    

    For the validator use

    @Html.ValidationMessage("Cat")
    

提交回复
热议问题