Asp.net mvc MVC:如何从模型对象中选择要下拉的特定列/字段

Asp.net mvc MVC:如何从模型对象中选择要下拉的特定列/字段,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,我在视图中有以下模型和简单下拉列表。我想在下拉列表中填充客户名称。我尝试了下面的代码,但它不起作用,任何人都可以告诉我如何使用dropdownselect语句指定列 型号 public class customer { public int id { get; set; } public string name { get; set; } public string address { get; set; } } //////////////////////////

我在视图中有以下模型和简单下拉列表。我想在下拉列表中填充客户名称。我尝试了下面的代码,但它不起作用,任何人都可以告诉我如何使用dropdownselect语句指定列

型号

public class customer
{

    public int id { get; set; }
    public string name { get; set; }
    public string address { get; set; }

}
//////////////////////////

  @{
Layout = null;
}

@model IEnumerable<MvcApplication1.customer>

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>

    @Html.DropDownList("name",Model);

</div>
@{
布局=空;
}
@模型IEnumerable
指数
@Html.DropDownList(“名称”,型号);
您可以使用:

更新:

您的模型没有name属性。它只包含具有name属性的对象。您应该更新模型以反映这一点:

class MyViewModel
{
    public IEnumerable<customer> CustomerList { get; set; }
    public string SelectedCustomer { get; set; }
}
然后可以创建下拉列表:

@Html.DropDownListFor(m => m.SelectedCustomer , new SelectList(Model.CustomerList ,"id","name"));

如何使用DropDownListFor进行此操作?我尝试了以下内容,但没有成功:(@Html.DropDownListFor(m=>m.name,newselectlist(Model,“id”,“name”));
@model MyViewModel
@Html.DropDownListFor(m => m.SelectedCustomer , new SelectList(Model.CustomerList ,"id","name"));