我是一个使用MVC方法的新手。如何在MVC中使用DropDownListfor?我需要从主表中获取列表,并将选项显示到Business Profile对象中的Business Premise中。这是我的ViewModel
public class SMEAppViewModel
{
public WP2PBusinessProfile BuisinessProfile { get; set; }
public IEnumerable<WP2PMasterDropDownList> MasterList { get; set; }
}在我的控制器中,我已经初始化了列表中的MasterList
var _MasterList = _context.WP2PMasterDropDownList.ToList();在我的视图中,我可以使用以下代码显示列表中的所有选项
@foreach (var karim in Model.MasterList.Where(c => c.Variable == "BusinessPremise"))
{
@Html.DisplayFor(modelitem => karim.Value) <br>
}然而,我在我的视图中使用了以下下拉列表,但是我的dropdownlistfor不能在我的视图中显示我的下拉选项。
@Html.DropDownListFor(m => m.BuisinessProfile.BusinessPremise, new SelectList(Model.MasterList, "Id", "Value"), "Select value")
@Html.LabelFor(m => m.BuisinessProfile.BusinessPremise, new { @class = "form-control" })有人会告诉我我的代码中有什么错误吗?
谢谢
发布于 2017-08-08 16:54:18
在视图中尝试此操作
@Html.DropDownListFor(m => m.Entity, new Project.Models.My_Class().My_Method, new { @class = "form-control" })你不能从你的DropDownListFor获取数据。
在My_Method中,您可以使用SelectList
https://stackoverflow.com/questions/41297883
复制相似问题