如何编辑ViewModels数据并将其保存回数据库

Jaa*_*aan 9 asp.net-mvc entity-framework asp.net-mvc-4

我有一个ViewModel,它由三个实体连接起来,将所有实体的数据转换为一个视图形式.虽然我成功实现了同样的目标.但我不知道如何编辑和保存数据回数据库.我的模型类由一对一的关系加入.

我的模特是:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我的ViewModel是:

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

    }
Run Code Online (Sandbox Code Playgroud)

我的控制器是:

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

    }
Run Code Online (Sandbox Code Playgroud)

我的观点是:

@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>}
Run Code Online (Sandbox Code Playgroud)

我的控制器类是:

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);
    }
Run Code Online (Sandbox Code Playgroud)

请帮帮我怎么办.提前致谢.

SOf*_*tic 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
Run Code Online (Sandbox Code Playgroud)

然后你的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);
}
Run Code Online (Sandbox Code Playgroud)

如果你想保留你的 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);
}
Run Code Online (Sandbox Code Playgroud)