在实体框架中加载下拉列表 [英] Loading dropdownlist in entity framework
问题描述
您好,我是MVC实体框架的新手
我创建了我的模型类Faculty and Department。
public class 教师
{
[Key]
public int FacultyID { get ; set ; }
[必需(ErrorMessage = 输入教职员名称)]
[显示(名称= 教职员名称)]
public string FacultyName { get ; set ; }
public virtual ICollection< Department>部门{获取; set ; }
}
public class 部门
{
[Key]
public int Id { get ; set ; }
// [必需(ErrorMessage =输入部门ID)]
// [显示(名称=部门ID)]
// public string departmentID {get;组; }
[必需(ErrorMessage = 输入部门名称)]
[显示(名称= 部门名称)]
public string DepartmentName { get ; set ; }
public virtual 教职员工{获得跨度>; set ; }
[必需(ErrorMessage = 选择教师 )]
[显示(名称= 教师)]
public int FacultyID { get ; set ; }
}
所以我生成了两个模型的视图
创建部门的教师ID字段如下所示。
< div class =form-group>
@ Html.LabelFor(model => model.FacultyID,FacultyID,htmlAttributes:new {@class =control-label col-md-2})
< div class = COL-MD-10\" >
@ Html.DropDownList(FacultyID,null,htmlAttributes:new {@class =form-control})
@ Html.ValidationMessageFor(model => model.FacultyID,,new {@class =text-danger})
< / div>
< / div>
那么如何使用viewbag来显示列表在视图的下拉列表中的院系?谢谢
我尝试了什么:
我试图使用Viewbag在部门创建行动
<前一个=C#> 公共 ActionResult创建()
{
ViewBag .FacultyID = new SelectList(db.Faculties, FacultyID , FacultyName);
return View();
}
我建议通过模型传递此信息。另外,我认为你不能在MVC中使用SelectList用于DropDownListFor,我认为你必须使用ListList of SelectListItem(或IEnumerable)。
所以语法对于DropDownListFor可以这么简单
@ Html.DropDownListFor(m => m.Name, new 列表< SelectListItem>(), new {@ class = form-control})
m => m。 Name是您的模型的属性名称和新List< selectlistitem>是您在下拉列表中显示的项目集合。但请注意,如果您未在视图中使用模型,则无法使用DropDownListFor,相反,只需DropDownList即可,因为它不是强类型的。
@ Html.DropDownList( 名称, new List< SelectListItem>(), new {@ class = form-control})
更实用的例子是类似于以下的东西
控制器动作
public ActionResult Index()
{
var list = new 列表< People>();
list.Add( new People {Name = 大卫});
list.Add( new People {Name = Thom});
var selectList = new List< SelectListItem>();
foreach ( var item 在列表中)
{
selectList.Add( new SelectListItem {Text = item.Name,Value = item.Name}) ;
}
return 查看(selectList);
}
查看 - Index.cshtml
< pre lang =HTML> @ model IEnumerable < SelectListItem >
@ {
ViewBag.Title =主页;
}
@ Html.DropDownList(名称,模型,新{@ class =form-control})
但是要使用viewbag,你会做同样的事情,但不是模特你只是做
控制器< br $> b $ b
public ActionResult Index()
{
var list = new List< People>();
list.Add( new People {Name = 大卫});
list.Add( new People {Name = Thom});
var selectList = new List< SelectListItem>();
foreach ( var item 在列表中)
{
selectList.Add( new SelectListItem {Text = item.Name,Value = item.Name}) ;
}
ViewBag.Items = selectList;
return 查看();
}
@ Html.DropDownList(Name,ViewBag.Items,new {@ class =form-control})
Quote:由David_Wimbley执导,创建强类型视图很适合...
如果对使用强类型视图感兴趣,请按照以下3个步骤操作下面;
1.创建一个将保存部门和教师名单的视图模型
public 类 DepartmentViewModel
{
public 部门部门{获得跨度>; set ; }
public 列表< Faculty>教师{获取; set ; }
}
2.控制器将是
public ActionResult Create()
{
// 假设教师列表是在其他地方创建的
var DepartmentViewModel = new DepartmentViewModel {Faculty = faculties};
return 查看(DepartmentViewModel);
}
3.并且视图将是(如果需要,此下拉列表也可以创建为部分视图)
@model DropDownListDemo.Models.DepartmentViewModel
@using(Html.BeginForm())
{
@ Html.AntiForgeryToken( )
< div class =form-horizontal>
@ Html.DropDownListFor(model => model.Department.FacultyID,new SelectList(Model.Faculty,FacultyID,FacultyName));
< / div>
}
Hello, I am new to Entity Framework with MVC
I created my model classes Faculty and Department.
public class Faculty
{
[Key]
public int FacultyID { get; set; }
[Required(ErrorMessage = "Enter Faculty Name")]
[Display(Name = "Faculty Name")]
public string FacultyName { get; set; }
public virtual ICollection<Department> Departments { get; set; }
}
public class Department
{
[Key]
public int Id { get; set; }
//[Required(ErrorMessage ="Enter Department ID")]
//[Display(Name ="Department ID")]
//public string departmentID { get; set; }
[Required(ErrorMessage = "Enter Department Name")]
[Display(Name = "Department Name")]
public string DepartmentName { get; set; }
public virtual Faculty Faculty { get; set; }
[Required(ErrorMessage = "Select Faculty")]
[Display(Name = "Faculty")]
public int FacultyID { get; set; }
}
So i generated a view for the two models
The field for faculty id in create department looks like this.
<div class="form-group"> @Html.LabelFor(model => model.FacultyID, "FacultyID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("FacultyID", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.FacultyID, "", new { @class = "text-danger" }) </div> </div>
So how do i use the viewbag to display the list of faculties in the dropdownlist in the view? Thanks
What I have tried:
I tried to use Viewbag in the Department Create action
public ActionResult Create()
{
ViewBag.FacultyID = new SelectList(db.Faculties, "FacultyID", "FacultyName");
return View();
}
I would recommend passing this via model. Also, I don't think you can use SelectList in MVC for DropDownListFor, i think you have to use a List of SelectListItem (or IEnumerable).
So the syntax for DropDownListFor can be something as simple as this
@Html.DropDownListFor(m=>m.Name, new List<SelectListItem>(), new{@class="form-control"})
m=>m.Name is the property name for your model and new List<selectlistitem> is your collection of items to display in the drop down. But notice, if you are not using a model in your view, you can't use DropDownListFor, instead, simply DropDownList will work as it is not strongly typed.
@Html.DropDownList("Name", new List<SelectListItem>(), new{@class="form-control"})
A more working example would be something similar to the following
Controller Action
public ActionResult Index() { var list = new List<People>(); list.Add(new People { Name = "David" }); list.Add(new People { Name = "Thom" }); var selectList = new List<SelectListItem>(); foreach (var item in list) { selectList.Add(new SelectListItem { Text = item.Name, Value = item.Name}); } return View(selectList); }
View - Index.cshtml
@model IEnumerable<SelectListItem> @{ ViewBag.Title = "Home Page"; } @Html.DropDownList("Name", Model, new{@class="form-control"})
But to use viewbag, you would do the same thing, but instead of model you just do
Controller
public ActionResult Index() { var list = new List<People>(); list.Add(new People { Name = "David" }); list.Add(new People { Name = "Thom" }); var selectList = new List<SelectListItem>(); foreach (var item in list) { selectList.Add(new SelectListItem { Text = item.Name, Value = item.Name}); } ViewBag.Items = selectList; return View(); }
@Html.DropDownList("Name", ViewBag.Items, new{@class="form-control"})
Quote:Rightly directed by David_Wimbley, creating strongly typed view is good to go for...
In case interested in using strongly typed view follow 3 steps as below;
1. create a viewmodel that will hold Department and Faculty list
public class DepartmentViewModel { public Department Department { get; set; } public List<Faculty> Faculty { get; set; } }
2. Controller will be
public ActionResult Create() { //assuming faculty list is created somewhere else var DepartmentViewModel = new DepartmentViewModel { Faculty=faculties}; return View(DepartmentViewModel); }
3. and view will be (if needed this dropdown list can also be created as partial view)
@model DropDownListDemo.Models.DepartmentViewModel @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> @Html.DropDownListFor(model => model.Department.FacultyID, new SelectList(Model.Faculty, "FacultyID", "FacultyName")); </div> }
这篇关于在实体框架中加载下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!