1

我有一个列表,我想将此列表填充到下拉框中。

从控制器:

public ActionResult Index()
        {
            List<SelectListItem> hum = allHuman();
            return View();

        }

从视图:

@Html.DropDownListFor( ..... // NOW HOW CAN I DISPLAY THE VALUES IN THE DROPDOWNLIST ?
4

2 回答 2

0

控制器

var list = new SelectList(allHuman(), "Id", "Name");

看法

@Html.DropDownList("humans", Model as SelectList)
于 2013-02-18T21:24:46.720 回答
0

您应该首先定义模型类型。

class MyViewModel
{
    // I assume you will use this property to read the selected value 
    public string Human {get;set;}

    //dropdown values
    public List<SelectListItem> AllHumans {get;set;}
}

然后在你的控制器中创建一个实例:

public ActionResult Index()
{
    var model = new MyViewModel { AllHumans  =  allHuman()};
    return View(model);
}

然后你可以在你的视图中显示它:

@model MyViewModel

@Html.DropDownListFor(model => model.Human, Model.AllHumans, null)
于 2013-02-18T21:27:55.417 回答