Populate DropDownList in MVC 5

前端 未结 3 595
-上瘾入骨i
-上瘾入骨i 2021-02-15 17:33

Here is my code for my AddNewProductViewModel

using AccessorizeForLess.Data;
using System.Collections.Generic;
using System.ComponentModel.DataA         


        
3条回答
  •  深忆病人
    2021-02-15 18:19

    Use your controller to build the list, then call the list from the view.

    Controller:

    public static List GetDropDown()
        {
            List ls = new List();
            lm = (call database);
            foreach (var temp in lm)
            {
                ls.Add(new SelectListItem() { Text = temp.name, Value = temp.id });
            }
            return ls;
        }
    

    Call the Dropdown:

    @Html.DropDownListFor(x => x.Field, PathToController.GetDropDown())
    

提交回复
热议问题