我试图在我的一个CRUD屏幕中创建一个基于程序中不同模型的下拉列表。现在,它根据ID输入值,但我希望它能够在下拉列表中填充讲师的姓名。
我已经使用选择列表尝试了一些其他操作,但似乎没有用。
这是我的模特
[Table("Section")]
public class Section
{
[Key]
public int Id { get; set; }
[DisplayName("Section")]
public int? section { get; set; }
public Nullable <int> instructor_id { get; set; }
public int location_id { get; set; }
public int modality_id { get; set; }
public int DOW_id { get; set; }
public int course_id { get; set; }
public virtual DOW DOW { get; set; }
public virtual Instructor Instructor { get; set; }
public virtual Location Location { get; set; }
public virtual Modality Modality { get; set; }
public virtual Course Course { get; set; }
这是我的控制者:
// GET: Sections/Create
public ActionResult Create()
{
return View();
}
// POST: Sections/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,section,startTime,endTime,startDate,endDate,isTap,isActive,instructor_id,location_id,modality_id,DOW_id,course_id")] Section section)
{
if (ModelState.IsValid)
{
db.Sections.Add(section);
db.SaveChanges();
return RedirectToAction("Index");
}
//ViewBag.instruct = new SelectList(db.Instructors, "Id", "lname", section.instructor_id);
return View(section);
}
这是我的观点
<div class="form-group">
@Html.LabelFor(model => model.instructor_id, "instructor_id" , htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.instructor_id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.instructor_id, "", new { @class = "text-danger" })
</div>
</div>
答案 0 :(得分:0)
以表格John Peters的答案,请看
创建一个数据层,以检索所需列表。然后使用EF获取所有状态。
//assuming you have a table of states..
var states = db.States();
状态表应该是唯一的状态列表。
var selectList = new List<SelectListItem>();
foreach(var thing in states){
//if you got everything, thus the ID field for the value...
selectList.Add(new SelectListItem {Text =thing.State, Selected = false, Value = thing.ID);
}
确保在Viewmodel类中,选择列表是公共属性.....并设置为您在上方所做的操作。您还需要为返回的视图选择提供一个字符串。
StatesSelectList = selectList;
public IEnumberable<SelectListItem> StatesSelectList {get;set;}
public string SelectedState {get;set;}
在您看来,请执行以下操作:
@Html.DropDownListFor(p=>Model.SelectedState, Model.StatesSelectList)
答案 1 :(得分:0)
您需要按以下方式更改Controller中的Get方法:
// GET: Sections/Create
public ActionResult Create()
{
ViewBag.Instructors = db.Instructors.ToList();
return View();
}
您的HTML需要进行如下更改:
<div class="form-group">
@Html.LabelFor(model => model.instructor_id, "instructor_id" , htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.instructor_id, new SelectList(ViewBag.Instructors, "Id", "Name"), new { @class = "form-control" } )
@Html.ValidationMessageFor(model => model.instructor_id, "", new { @class = "text-danger" })
</div>
</div>
确保字段名称正确。