【发布时间】:2019-06-24 11:51:30
【问题描述】:
我正在尝试在我的一个 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>
【问题讨论】:
-
请参考 John Peters 的 stackoverflow.com/a/26663526/7262120
标签: c# asp.net-mvc crud viewbag