我正在尝试将 Dropdownlistfor 添加到我的 MVC 项目中,但没有成功。我想显示客户列表。我希望显示客户并选择 ID。
我正在努力将我的列表投射到 selectListItems 中,有人可以帮我解决这个问题。
控制器
public ActionResult createNewUser()
{
List<string> Customers = DBH.CustomerBranchGetAll();
var Model = new UserModel
{
Customers = Customers
};
return View(Model);
}
模型
public class UserModel
{
public int CustomerId { get; set; }
public List<string> Customers { get; set; }
public IEnumerable<SelectListItem> SelectCustomers
{
get { return new SelectList(Customers, "Id", "Name"); }
}
}
看法
<div class="editor-label">
@Html.Label("Choose Your Customer name")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CustomerId, Model.SelectCustomers)
@Html.ValidationMessageFor(model => model.Customers)
</div>