Entity framework MVC3在强类型视图中自定义DropdownList项
我在强类型视图中有一个从实体框架自动生成的DropDownList:Entity framework MVC3在强类型视图中自定义DropdownList项,entity-framework,asp.net-mvc-3,Entity Framework,Asp.net Mvc 3,我在强类型视图中有一个从实体框架自动生成的DropDownList: <div class="editor-field"> @Html.DropDownList("User_FK", String.Empty) @Html.ValidationMessageFor(model => model.User_FK) </div> 我需要此列表来显示从Active Directory加载的人员的姓名。如何单独自定义选择列表选项?@Html。DropDo
<div class="editor-field">
@Html.DropDownList("User_FK", String.Empty)
@Html.ValidationMessageFor(model => model.User_FK)
</div>
我需要此列表来显示从Active Directory加载的人员的姓名。如何单独自定义选择列表选项?
@Html。DropDownList
有另一个重载,即:
public static MvcHtmlString DropDownList(
this HtmlHelper htmlHelper,
string name,
IEnumerable<SelectListItem> selectList
)
通过视图模型模式ViewBag将列表传递给视图
然后,将其传递到视图后(最好通过视图模型而不是ViewBag)
CreateViewModel()不存在,或者我不知道在哪里可以找到它。有什么我遗漏的吗?@Tjaart抱歉——我在假设类型列表中忘了提到这一点。它是一个普通类,包含一个名为
MyList
的List
属性——它只是一种包装发送到视图的模型的方法。因此,“视图模型”在我的视图中看不到MyList。可能是因为我的模型在我的视图中已经被定义为@model SystemInformationManager.Models.systemmainter
@Tjaart,根据您的Create
操作方法,我假设没有模型<代码>返回视图()代码>不向视图发送模型。你能进入SystemMaintainer
吗?如果没有,您可能必须将其包装在CreateViewModel
中,并修改使用SystemMaintainer属性的任何现有视图代码。确定。我让它工作了。我必须将控制器操作中的selectlist更改为ViewBag.User_FK=new selectlist(用户,“ID”,“ADName”)代码>其中用户是我的自定义用户类型。对象未传递给视图的原因是,Create正在加载要插入到systemmainter表中的表单SystemMaintainer
可以访问,但作为新对象。您是否愿意编辑您的答案以反映有效的答案。我将修改这个问题,以明确它是一个强类型视图。非常感谢。
public static MvcHtmlString DropDownList(
this HtmlHelper htmlHelper,
string name,
IEnumerable<SelectListItem> selectList
)
List<MyUserType> users = new List<MyUserType>();
foreach(var user in MyActiveDirectoryRepository.GetUsers())
{
users.Add(new MyUserType()
{
ADName = user.Username,
ID = user.ID // or SAM token, or something similar
});
}
public ActionResult Create()
{
// populate list as above
ViewBag.User_FK = new SelectList(users, "ID", "ADName");
return View();
}
@Html.DropDownList("NameOfDropDown", ViewBag.User_FK)