11

すべてのエンティティからのデータを 1 つのビュー フォームに取得するために、3 つのエンティティが結合された ViewModel があります。私は同じことを実装することに成功しましたが。しかし、データを編集してデータベースに保存する方法がわかりません。私のモデル クラスは 1 対 1 の関係で結合されています。

私のモデルは次のとおりです。

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; }
}

私のViewModelは次のとおりです。

public class DoctorViewModel
    {
        public Doctor Doctor { get; set; }
        public DoctorAddress DoctorAddress { get; set; }
        public DoctorCharge DoctorCharge { get; set; }

    }

私のコントローラーは:

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());

    }

私の見解は:

@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>}

私のコントローラークラスは次のとおりです。

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);
    }

どうすればいいのか教えてください。前もって感謝します。

4

3 に答える 3

4

まず第一に、あなたが使っていることは本当に良いことですがViewModels、この特定のケースでは、おそらく必要ではありません.Createビューは次のようになります:

@model MvcApplication1.Models.Doctor

//other fields here

<div class="editor-label">
        @Html.LabelFor(model => model.DoctorAddress.Address)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DoctorAddress.Address)
        @Html.ValidationMessageFor(model => model.DoctorAddress.Address)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.DoctorCharge.IPDCharge)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DoctorCharge.IPDCharge)
        @Html.ValidationMessageFor(model => model.DoctorCharge.IPDCharge)
</div>

//other fields here

次に、Doctorコントローラー:

[HttpPost]
public ActionResult Create(Doctor doctor)
{
    if (ModelState.IsValid)
    {
        db.Doctors.Add(doctor);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(doctor);
}

Your `Edit` action could then look like this:

[HttpGet]
public ActionResult Edit(int id = 0)
{
    Doctor doctor = db.Doctors.Find(id);
    if (doctor == null)
    {
        return HttpNotFound();
    }
    return View(doctor);
}

[HttpPost]
public ActionResult Edit(Doctor doctor)
{
    if (ModelState.IsValid)
    {
        db.Entry(doctor).State = EntityState.Modified;
        db.Entry(doctor.DoctorAddress).State = EntityState.Modified;
        db.Entry(doctor.DoctorCharge).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(doctor);
}

ViewModel を保持したい場合は、次のようになります。

[HttpPost]
public ActionResult Edit(DoctorViewModel doctorViewModel)
{
    if (ModelState.IsValid)
    {
        var doctorAddress = doctorViewModel.DoctorAddress;
        var doctorCharge = doctorViewModel.DoctorCharge;
        var doctor = doctorViewModel.Doctor;
        db.Entry(doctorAddress).State = EntityState.Modified;
        db.Entry(doctorCharge).State = EntityState.Modified;
        db.Entry(doctor).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(doctor);
}
于 2013-08-14T18:39:41.353 に答える