如何绑定MVC中的Razor视图HTML下拉列表 [英] How to bind HTML Drop Down List in MVC Razor view
本文介绍了如何绑定MVC中的Razor视图HTML下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目,我得到IEnumerable集合,现在想这个绑定我的HTML下拉列表
我的Razor视图code是:
in my project i'm getting IEnumerable collection, now want to bind this my Html Drop Down list my razor view code is as:
@if (Model.LanguageNavigationLinkItem != null)
{
// drop down list item Collection
var ddlItem = Model.LanguageNavigationLinkItem;
@Html.DropDownList(ddlItem.ToList(),"-- Select Item --")
}
我无法在此集合绑定我的下拉列表中,请任何一个帮助我。
i couldn't bind this collection with my drop down list please any one help me.
推荐答案
下面是一个使用绑定下拉 ViewBag
的例子。您还可以使用模式以类似的方式绑定下拉列表。
Here is an example to bind dropdown using ViewBag
. You can also use model to bind dropdown in similar way.
控制器code
//Getting list of employees from DB.
var list = ent.Employees.SqlQuery(ent.Queries.FirstOrDefault().Query1).ToList<Employee>();
List<SelectListItem> selectlist = new List<SelectListItem>();
foreach (Employee emp in list)
{
selectlist.Add(new SelectListItem { Text = emp.Name, Value = emp.Id.ToString() });
}
ViewBag.SelectList = selectlist;
查看
@Html.DropDownList("name",(IEnumerable<SelectListItem>)ViewBag.SelectList)
这篇关于如何绑定MVC中的Razor视图HTML下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文