Mam ViewModel, który jest połączony z trzema elementami, aby pobrać dane ze wszystkich obiektów do jednego formularza widoku. Chociaż udało mi się wprowadzić to samo. Ale nie mam pojęcia, jak edytować i zapisywać dane z powrotem do bazy danych. Moje klasy modeli są połączone relacją jeden do jednego.Jak edytować i zapisywać dane ViewModels z powrotem do bazy danych
Moje modele to:
public class Doctor
{
public int DoctorId { get; set; }
public string Name { get; set; }
public string Speciality { get; set; }
public virtual DoctorAddress DoctorAddress { get; set; }
public virtual DoctorCharge DoctorCharge { get; set; }
public virtual DoctorAvailablity DoctorAvailablity { get; set; }
}
public class DoctorAddress
{
public string Address { get; set; }
public string City { get; set; }
public int DoctorId { get; set; }
public virtual Doctor Doctor { get; set; }
}
public class DoctorCharge
{
public decimal OPDCharge { get; set; }
public decimal IPDCharge { get; set; }
public int DoctorId { get; set; }
public virtual Doctor Doctor { get; set; }
}
My ViewModel jest:
public class DoctorViewModel
{
public Doctor Doctor { get; set; }
public DoctorAddress DoctorAddress { get; set; }
public DoctorCharge DoctorCharge { get; set; }
}
My Kontroler jest:
public ActionResult Index()
{
var model = from t1 in db.Doctors
join d in db.DoctorAddress on t1.DoctorId equals d.DoctorId into listi
join dc in db.DoctorCharges on t1.DoctorId equals dc.DoctorId into listj
from d in listi.DefaultIfEmpty()
from dc in listj.DefaultIfEmpty()
select new DoctorDetailsViewModel.DoctorViewModel { Doctor = t1, DoctorAddress = d, DoctorCharge = dc };
return View(model.ToList());
}
moim zdaniem jest:
@model XXX.DoctorDetailsViewModel.DoctorViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Doctor</legend>
<div class="editor-label">
Name
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Doctor.Name)
</div>
<div class="editor-label">
OPD Charge
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DoctorCharge.OPDCharge)
</div>
<div class="editor-label">
Address
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DoctorAddress.Address)
</div> <p>
<input type="submit" value="Create" />
</p>
</fieldset>}
moja klasa kontrolera jest:
public ActionResult Create()
{
return View();
}
//
// POST: /Doctor/Create
[HttpPost]
public ActionResult Create(Doctor doctor)
{
if (ModelState.IsValid)
{
db.Doctors.Add(doctor);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(doctor);
}
Proszę mi pomóc jak mogę zrobić. Z góry dziękuję.
Po prostu to dostałeś. Twoja metoda działania otrzymująca POST powinna otrzymać ten sam typ co model widoku, tj. 'DoctorViewModel'. Wiązanie modelu struktury MVC automatycznie zamapuje pola wejściowe na odpowiednie właściwości w modelu widoku na podstawie ich nazw. – asymptoticFault
W jaki sposób podmioty Doctor i DoctorCharge mogą mieć relację jeden-do-jednego? – ataravati
@ataravati Klasa Doctor ma właściwość DoctorCharge i vice versa. – asymptoticFault