ちょっとこれは、3つのテーブルを結合した私のモデルです。
public Student Student { get; set; }
public Simester Semester { get; set; }
public Lesson Lesson { get; set; }
そして、これは私のコントローラーです:
[HttpGet]
public ActionResult UnitSelection()
{
var students = context.Students;
ViewBag.Students = new SelectList(students, "Id", "FullName");
var semesters = context.Simesters;
ViewBag.Semesters = new SelectList(semesters, "Id", "Title");
var lessons = context.Lessons;
ViewBag.Lessons = new SelectList(lessons, "Id", "Title");
return View();
}
[HttpPost]
public ActionResult UnitSelection(UnitSelectionViewModel model)
{
StudnetLesson snl = new StudnetLesson();
snl.LessonId = model.Lesson.Id;
snl.StudentId = model.Student.Id;
snl.SimesterId = model.Semester.Id;
context.StudnetLessons.Add(snl);
context.SaveChanges();
return RedirectToAction("Success");
}
foreach ループのビューにチェックボックス リストを表示したい。チェックボックスリスト(その文字列)にレッスンを表示したい
public Lesson Lesson { get; set; }
多くのコードをテストしましたが、うまくいきませんか?
@using (Html.BeginForm("UnitSelection", "Home", FormMethod.Post))
{
<text>Student Name: </text>
@Html.DropDownListFor(x => x.Student.Id, (SelectList)ViewBag.Students, "Choose a student")
<br />
<text>Semester: </text>
@Html.DropDownListFor(x => x.Semester.Id, (SelectList)ViewBag.Semesters, "Choose your semester")
<br />
<text>Lesson: </text>
foreach (var lessons in (SelectList) ViewBag.Lessons)
{
}
}