2017-04-15 72 views
0

我會問你關於我的項目時間表 目前我正在學習這種技術 所以請幫我ASP.Net MVC如何多行保存到數據庫

此圖像中選擇一組:

我想在這個表中的每個字段添加到數據庫中,然後顯示在標籤:

這是我的控制器

using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Net; 
using System.Web.Mvc; 
using TimeTable2.Models; 
using System.Collections.Generic; 
using System; 

namespace TimeTable2.Controllers 
{ 
    public class TablesController : Controller 
    { 
     private table_systemEntities1 db = new table_systemEntities1(); 


     [HttpPost] 
     [ValidateAntiForgeryToken] 


     public ActionResult TheTables() 
     { 
      PopulateDepartmentsDropDownList(); 
      PopulateClassroomsDropDownList(); 
      PopulateCoursesDropDownList(); 
      PopulateTeachersDropDownList(); 
      PopulateFacultiesDropDownList(); 
      PopulateLevelsDropDownList(); 

      return View(); 
     } 
     [HttpPost] 
     [ValidateAntiForgeryToken] 

     public JsonResult TheTables([Bind(Include = "TabelsID,TeacherID,CourseID,LevelID,DepartmentID,ClassRoomID,TimesID,DaysID,Years,Semester,Measure")] Tabels tabels) 
     { 
      try 
      { 
       if (ModelState.IsValid) 
       { 
        db.Tabels.Add(tabels); 
        db.SaveChanges(); 
       } 
      } 
      catch (DataException /* dex */) 
      { 
       ModelState.AddModelError("", "Unable to save changes. Try again ."); 
      } 
      ViewBag.ClassRoomID = new SelectList(db.Class_Room, "ClassRoomID", "Name", tabels.ClassRoomID); 
      ViewBag.DaysID = new SelectList(db.Days, "DayID", "Name", tabels.DaysID); 
      ViewBag.LevelID = new SelectList(db.Level, "LevelID", "Name", tabels.LevelID); 
      ViewBag.TeacherID = new SelectList(db.Teacher, "TeacherID", "Name", tabels.TeacherID); 
      ViewBag.TimesID = new SelectList(db.Times, "TimesID", "Name", tabels.DaysID); 
      ViewBag.CourseID = new SelectList(db.Course, "CourseID", "Name", tabels.CourseID); 
      ViewBag.FacultyID = new SelectList(db.Faculty, "FacultyID", "Name", tabels.FacultyID); 
      ViewBag.DepartmentID = new SelectList(db.Department, "DepartmentID", "Name", tabels.DepartmentID); 
      ViewBag.LevelID = new SelectList(db.Level, "LevelID", "Name", tabels.LevelID); 


      return Json(tabels, JsonRequestBehavior.AllowGet); 

     } 

     public ActionResult DetailsofTable() 
     { 

      PopulateDepartmentsDropDownList(); 
      PopulateClassroomsDropDownList(); 
      PopulateCoursesDropDownList(); 
      PopulateTeachersDropDownList(); 
      PopulateFacultiesDropDownList(); 
      PopulateLevelsDropDownList(); 


      return View(); 
     } 
     public ActionResult CreateTheTable() 
     { 

      PopulateDepartmentsDropDownList(); 
      PopulateClassroomsDropDownList(); 
      PopulateCoursesDropDownList(); 
      PopulateTeachersDropDownList(); 
      PopulateFacultiesDropDownList(); 
      PopulateLevelsDropDownList(); 

      return View(); 
     } 
     public JsonResult GetDepartmentById(int ID) 
     { 
      db.Configuration.ProxyCreationEnabled = false; 
      return Json(db.Department.Where(p => p.FacultyID == ID), JsonRequestBehavior.AllowGet); 
     } 
     public JsonResult GetTeacherById(int ID) 
     { 
      db.Configuration.ProxyCreationEnabled = false; 
      return Json(db.Teacher_Course.Where(p => p.CourseID == ID), JsonRequestBehavior.AllowGet); 
     } 
     public JsonResult GetFacultyById(int ID) 
     { 
      db.Configuration.ProxyCreationEnabled = false; 
      return Json(db.Class_Room.Where(p => p.ClassRoomID == ID), JsonRequestBehavior.AllowGet); 
     } 
     public JsonResult GetCoursesById(int ID) 
     { 
      db.Configuration.ProxyCreationEnabled = false; 
      return Json(db.Teacher_Course.Where(p => p.DepartmentID == ID), JsonRequestBehavior.AllowGet); 
     } 

     private void PopulateDepartmentsDropDownList(object selectedDepartment = null) 
     { 
      var departmentsQuery = from d in db.Department 
            orderby d.Name 
            select d; 
      ViewBag.DepartmentID = new SelectList(departmentsQuery, "DepartmentID", "Name", selectedDepartment); 
     } 
     private void PopulateClassroomsDropDownList(object selectedClassRooms = null) 
     { 
      var classroomsQuery = from d in db.Class_Room 
            orderby d.Name 
            select d; 
      ViewBag.ClassRoomID = new SelectList(classroomsQuery, "ClassRoomID", "Name", selectedClassRooms); 
     } 
     private void PopulateCoursesDropDownList(object selectedCourses = null) 
     { 
      var coursesQuery = from d in db.Course 
            orderby d.Name 
            select d; 
      ViewBag.CourseID = new SelectList(coursesQuery, "CourseID", "Name", selectedCourses); 
     } 
     private void PopulateTeachersDropDownList(object selectedTeachers = null) 
     { 
      var teacherQuery = from d in db.Teacher 
            orderby d.Name 
            select d; 
      ViewBag.TeacherID = new SelectList(teacherQuery, "TeacherID", "Name", selectedTeachers); 
     } 
     private void PopulateFacultiesDropDownList(object selectedFaculties = null) 
     { 
      var facultyQuery = from d in db.Faculty 
           orderby d.Name 
           select d; 
      ViewBag.FacultyID = new SelectList(facultyQuery, "FacultyID", "Name", selectedFaculties); 
     } 
     private void PopulateLevelsDropDownList(object selectedLevels = null) 
     { 
      var levelQuery = from d in db.Level 
           orderby d.Name 
           select d; 
      ViewBag.LevelID = new SelectList(levelQuery, "LevelID", "Name", selectedLevels); 
     } 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 
    } 
} 

所以我怎樣才能在數據庫中保存多個領域的同時

對不起,我不能說英語

回答

1

我強烈建議你使用的EntityFramework,你可以讀到它here。 示例用法here。您可以非常快地設置它,並且有很多指南。

不知道如何用你的方式來做到這一點。

0

要在實體框架中插入多行,可以使用AddRange

db.Tabels.AddRange(tabels); 

請檢查AddRange

希望它幫助!

相關問題