如何使用多个下拉列表从多个表中选择数据,并将所选值插入mvc

时间:2016-05-03 12:15:00

标签: asp.net-mvc

控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Lecture timetable)
{

  Lecture t = new Lecture();
  ViewBag.SId = new SelectList(db.Sections, "Id", "SectionName");
  ViewBag.CId = new SelectList(db.Course, "Id", "CourseName");
  ViewBag.FId = new SelectList(db.Faculty, "Id", "FacultyName");

  if (ModelState.IsValid)
  {

   db.TimeTable.Add(timetable);
   db.SaveChanges();
   return RedirectToAction("Create");
   }

   return View(timetable);

}

型号:

[Table("Lecture")]
public class Lecture
{



    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Terms { get; set; }
    public string Semester { get; set; }


    public int SectionsId { get; set; }

    [ForeignKey("SectionsId")]
    public Sections Sections { get; set; }

    public int CourseId { get; set; }

    [ForeignKey("CourseId")]
    public Course Course { get; set; }

    public string CreditHourTheory { get; set; }

    public string CreditHourLab { get; set; }

    public int? LabInstructorId { get; set; }
    [ForeignKey("LabInstructorId")]
    public Faculty Labinstructor { get; set; }

    public int FacultyId { get; set; }
    [ForeignKey("FacultyId")]
    public Faculty Faculty { get; set; }

    public int RoomId { get; set; }
    [ForeignKey("RoomId")]
    public Rooms Rooms { get; set; }


    public string Day { get; set; }

    public DateTime Date { get; set; }

    public TimeSpan TimeStart { get; set; }

    public TimeSpan TimeEnd { get; set; }
}

查看:

@Html.DropDownList("SId", null, String.Empty, new { @class = "form-control", @required = "" })
@Html.DropDownList("CId", null , String.Empty, new { @class = "form-control select-section", @required = "" })
@Html.DropDownList("FId", null , String.Empty, new { @class = "form-control select-section", @required = "" })

This is the form with dropdown i want to select these values and after clicking the create button the selected values should be submitted in the table "Lecture" in database

我在数据库中插入所选值时遇到一些错误。请为我提供更好的解决方案。下拉列表中的值来自不同表中的数据库。

1 个答案:

答案 0 :(得分:0)

您的另一个功能使用属性[HttpGet]创建中的问题(或者没有任何属性,默认为[httpGet])。 ViewBag中的变量必须与类 Lecture SectionsId,CourseId,FacultyId的成员一起命名。

您应该重定向到修改操作。

所以,

控制器:

public ActionResult Create()
{
  ViewBag.SectionsId = new SelectList(db.Sections, "Id", "SectionName");
  ViewBag.CourseId = new SelectList(db.Course, "Id", "CourseName");
  ViewBag.FacultyId = new SelectList(db.Faculty, "Id", "FacultyName");

  return View(new Lecture());
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Lecture timetable)
{  
  ViewBag.SectionsId = new SelectList(db.Sections, "Id", "SectionName");
  ViewBag.CourseId = new SelectList(db.Course, "Id", "CourseName");
  ViewBag.FacultyId = new SelectList(db.Faculty, "Id", "FacultyName");

  if (ModelState.IsValid)
  {    
     db.TimeTable.Add(timetable);
     db.SaveChanges();
     return RedirectToAction("Edit");
   }

   return View(timetable);    
}

查看:

@Html.DropDownList("SectionsId", null, htmlAttributes: new {@id = "SectionsId", @class = "form-control"})
@Html.DropDownList("CourseId", null, htmlAttributes: new {@id = "CourseId", @class = "form-control"})
@Html.DropDownList("FacultyId", null, htmlAttributes: new {@id = "FacultyId", @class = "form-control"})