Jak zapisać viewmodel do kilku tabel z których brałem dane do utworzenia tego poszczególnych modeli a nastepnie viewmodelu?
Załóżmy że mam kilka modeli:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
public DateTime BirthAt { get; set; }
public int PlaceId { get; set; }
public int DeskId { get; set; }
public virtual Place Place { get; set; }
public virtual Desk Desk { get; set; }
}
public class Place
{
public int PlaceId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Desk
{
public int DeskId { get; set; }
public string Model { get; set; }
public string Description { get; set; }
public int Capacity { get; set; }
}
public class StudentViewModel
{
public int StudentId { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
public DateTime BirthAt { get; set; }
public int PlaceId { get; set; }
public string PlaceName { get; set; }
public int DeskId { get; set; }
public string DeskModel { get; set; }
public string DeskDescription { get; set; }
}
public class StudentService
{
public StudentViewModel GetStudentViewModel(int studentId)
{
using(var context = new StudentDbContext)
{
//pobranie danych z 3 tabel, przepisanie ich na StudentViewModel i zwrócenie StudentViewModel
}
}
public void Save(StudentViewModel model)
{
//jak zapisywać dane?
}
}
Mam klasy, zgodne z tabelami w bazie. W kontrolerze mam:
public class AdminController:Controller
{
public ViewResult Student(int studentId)
{
StudentViewModel model = studentService.GetStudentViewModel(studentId);
return View(model);
}
}
teraz jesli chcialbym edytować dane studenta zawarte ww ViewModelu (chciałbym mieć możliwość edycji istniejących danych ale też dodania nowych danych, czyli musi być możliwość zapisania do tabel Desk i Place nowych danych, których tam wcześniej nie było, jak i edycji istniejących w nich danych) Jak to się robi?
Myślałem nad dodaniem w studentService metody Save (tak jak to wyżej dodałem w klasie). Moja metoda Save przyjmie ViewModel i rozbije sobie go na składowe dla poszczególnych klas, a następnie obiekt każdej klasy zapisze we właściwej tabeli. Czyli myślę nad czymś takim:
public class AdminController : Controller
{
//...
[HttpPost]
public ActionResult AddEditStudentData(StudentViewModel studentViewModel)
{
studentService.Save(studentViewModel);
return View();
}
}
public void Save(StudentViewModel model)
{
Place place = new Place { Name = model.PlaceName, Description = model.PlaceDescription };
Desk desk = new Desk { Name = model.DeskName, Description = model.DeskDescription };
Student student = new Student { Name = model.Name, Lastname = model.LastName, Desk = desk, Place = place };
using (var context = new StudentDbContext())
{
context.Places.Add(place);
context.Desks.Add(desk);
context.Students.Add(students);
context.SaveChanges();
}
}
Czyli korzystam dalej z studentService gdzie rozbijam mój ViewModel na poszczególne modele i zapisuje je osobno do tabel (oczywiście muszę zrobić jakies sprawdzanie czy obiekt istnieje w bazie, wtedy robie update(ustaiwam EntityState.Modified) a jeśli go nie ma to robie instert (EntityState.Added)