如何绑定mvc4中的下拉列表 [英] how to bind a dropdownlist in mvc4
本文介绍了如何绑定mvc4中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何绑定mvc4中的下拉列表?
how to bind a dropdown in mvc4?
推荐答案
如果您使用的是Html.Dropdownlist(下拉列表的名称,下拉列表的值)。
代码视图:
@ Html.DropDownList(portname,(IEnumerable< SelectListItem>)ViewData [ items],new {@class =drpdwn})
控制器代码:
//
//创建一个对象dl来访问datatacess图层并获取数据集
Modelobj portret = new modelobj();
DAL dl = new DAL();
portret.pl = dl.GetPortList();
列表< selectlistitem> items = new List< selectlistitem>();
items = getitems(portret2);
ViewData [items ] = item;
public List< selectlistitem> getitems(Modelobj portret)
{
List< selectlistitem> items = new List< selectlistitem>();
foreach(portret.pl中的var项目)
{
items.Add(new SelectListItem
{
Text = Convert.ToString(item.Port_Name),
Value = Convert.ToString(item.Port_Name)
});
}
退货;
}
Modelobj中的代码(型号名称) ):
public List< modelobjects> pl = new List< modelobjects>();
Modelobjects中的代码(另一种型号):
公共类Modelobjects
{
// dropwdon列表中物品的属性
public string Port_Name {get;组; }
}
If you are using Html.Dropdownlist(Name of the dropdown list,Values of dropdownlist).
Code in view :
@Html.DropDownList("portname", (IEnumerable<SelectListItem>)ViewData["items"], new { @class = "drpdwn" })
Code in Controller :
//
// create an object dl to access datatacess layer and get the dataset
Modelobj portret=new modelobj();
DAL dl = new DAL();
portret.pl = dl.GetPortList();
List<selectlistitem>items = new List<selectlistitem>();
items = getitems(portret2);
ViewData["items"] = items;
public List<selectlistitem> getitems(Modelobj portret)
{
List<selectlistitem> items = new List<selectlistitem>();
foreach (var item in portret.pl)
{
items.Add(new SelectListItem
{
Text = Convert.ToString(item.Port_Name),
Value = Convert.ToString(item.Port_Name)
});
}
return items;
}
Code in Modelobj(Model Name):
public List<modelobjects> pl = new List<modelobjects>();
Code in Modelobjects (Another model) :
public class Modelobjects
{
//property of items inside dropwdon list
public string Port_Name { get; set; }
}
看看这个。
< a href =http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx> mvc中的Dropdownlistfor [ ^ ]
真有帮助
Check this out.
Dropdownlistfor in mvc[^]
It really helps
在控制器中
private void fillAllDDL()
{
使用(ExamEntities context = new ExamEntities())
{
var lstCourse = (来自上下文。课程
orderby t.Course_Name
select t)。ToArray();
ViewBag.course =新的SelectList(lstCourse,Course_Id,Course_Name);
}
查看
@ Html.DropDownListFor(m => m.course,(SelectList)ViewBag.course, - 选择课程 - ,新{@Id =ddlCourse,@ width =150px})
In controller
private void fillAllDDL()
{
using (ExamEntities context = new ExamEntities())
{
var lstCourse = (from t in context.Courses
orderby t.Course_Name
select t).ToArray();
ViewBag.course = new SelectList(lstCourse, "Course_Id", "Course_Name");
}
In View
@Html.DropDownListFor(m => m.course, (SelectList)ViewBag.course, "--Select Course--", new { @Id = "ddlCourse", @width = "150px"" })
这篇关于如何绑定mvc4中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文