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);
}
}
}
так как я могу сохранить более одного поля в базе данных одновременно
Извините я не могу говорить по английски
2 ответа
Чтобы вставить несколько строк в Entity Framework, вы можете использовать AddRange.
db.Tabels.AddRange(tabels);
Пожалуйста, проверьте AddRange
Надеюсь, поможет!!