ViewModel을 사용하여 여러 엔티티에 데이터를 저장하고 업데이트하는 방법을 도와 줄 수 있습니까?ASP.NET MVC 4를 사용하여 여러 테이블에 데이터 저장/업데이트
public class StudentViewModel
{
public Student student;
public StudentAddress studentAddress { get; set; }
public StudentPhoto studentPhoto { get; set; }
// Three entities are related to one to one relationship
public DoctorDetailsViewModel()
{ }
}
내 컨트롤러는 다음과 같습니다 :
나는이처럼 보이는 뷰 모델이 내가 검색하고보기에 (여러 기관에서) 데이터를 표시 할 수 있었다
[HttpPost]
public ActionResult Create(StudentViewModel studentViewModel)
{
if (ModelState.IsValid)
{
return View(studentViewModel);
}
Student s = new Student()
{
Name =studentViewModel.Student.Name,
Speciality = studentViewModel.Student.Speciality,
DateOfJoinig = studentViewModel.Student.DateOfJoinig,
Qualification = studentViewModel.Student.Qualification,
Email = studentViewModel.Student.Email
};
StudentAddress sa = new StudentAddress()
{
StudentId= studentViewModel.Student.StudentId,
Address = studentViewModel.StudentAddress.Address,
Area = studentViewModell.StudentAddress.Area,
City = studentViewModel.StudentAddress.City,
State = studentViewModel.StudentAddress.State,
Pincode = studentViewModel.StudentAddress.Pincode,
Phone = studentViewModel.StudentAddress.Phone,
Mobile = studentViewModel.StudentAddress.Mobile
};
StudentPhoto sp = new StudentPhoto()
{
StudentId= studentViewModel.Student.StudentId,
Photo = studentViewModel.StudentPhoto.Photo
};
db.Students.Add(s);
db.StudentAddress.Add(sa);
db.StudentPhoto.Add(sp);
db.SaveChanges();
return RedirectToAction("Home");
}
. 그러나 이제는 어떻게 위의 엔티티를 새로운 데이터로 저장하고 업데이트 할 수 있을지 고민 중입니다. 예제의 대부분은 매핑이 자동이지만,이 경우 데이터는 여러 엔터티에 속합니다.
가장 좋은 방법은 무엇입니까? 감사.
무엇이 문제입니까? 당신은 당신이하는 일을 보여줄뿐입니다. –