-1

我有一个 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 int DoctorId { get; set; }
    public string Address { get; set; }
    public string City { get; set; }

    public virtual Doctor Doctor { get; set; }
}

public class DoctorCharge
{
    public int DoctorId { get; set; }
    public decimal OPDCharge { get; set; }
    public decimal IPDCharge { get; set; }

    public virtual Doctor Doctor { get; set; }
}

我的视图模型是:

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


    //Doctor attributes
    public int DoctorId { get; set; }
    public string Name { get; set; }
    public string Speciality { get; set; }
    public DateTime DateOfJoinig { get; set; }

    //DoctorAddress attributes
    public string Address { get; set; }
    public string Area { get; set; }
    public string City { get; set; }

    //DoctorCharge attributes
    public decimal OPDCharge { get; set; }
    public decimal OPDRevisitCharge { get; set; }


}

我的控制器是:

[HttpPost]
    public ActionResult Create(DoctorDetailsViewModel doctorViewModel)
    {
        if (ModelState.IsValid)
        {
            var d = new Doctor()
             {
                 Name =doctorViewModel.Doctor.Name,
                 Speciality = doctorViewModel.Speciality,
                 DateOfJoinig = doctorViewModel.DateOfJoinig

             };

            var da = new DoctorAddress()
            {
                DoctorId = doctorViewModel.DoctorId,
                Address = doctorViewModel.Address,
                City = doctorViewModel.City
            };


            var dc = new DoctorCharge()
            {
                DoctorId = doctorViewModel.DoctorId,
                OPDCharge = doctorViewModel.OPDCharge,
                OPDRevisitCharge = doctorViewModel.OPDRevisitCharge
            };

            db.Doctors.Add(d);
            db.DoctorAddress.Add(da);
            db.DoctorCharges.Add(dc);

            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(doctorViewModel);
    }

我的观点是:

@model HospitalManagementSystem.Models.Doctor

@{
ViewBag.Title = "Create";
}

<fieldset>
<legend>Doctor</legend>

<div class="editor-label">
   Name
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
</div>
<div class="editor-label">
   Speciality 
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Speciality)
</div>
<div class="editor-label">
   DateOfJoinig 
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DateOfJoinig)
</div>
<div class="editor-field">
        @Html.HiddenFor(model => model.DoctorId)
</div>

<div class="editor-label">
    Address
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DoctorAddress.Address)
</div> 
<div class="editor-label">
    Area
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DoctorAddress.Area)
</div> 
<div class="editor-label">
    City
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DoctorAddress.City)
</div> 
<div class="editor-field">
        @Html.HiddenFor(model => model.DoctorId)
</div>

<div class="editor-label">
    OPD Charge
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DoctorCharge.OPDCharge)
</div>

<div class="editor-label">
    OPDRevisitCharge 
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DoctorCharge.OPDRevisitCharge)
</div>
<p>
    <input type="submit" value="Create" />
 </p>
</fieldset>        
 }

我在保存时收到错误消息:“传递到字典中的模型项是类型HM.ViewModels.DoctorDetailsViewModel,但是这个字典需要一个类型的模型项HM.Models.Doctor

请帮助我该怎么做。提前致谢。

4

1 回答 1

0

这一行:

@model HospitalManagementSystem.Models.Doctor

声称 View 需要一个 ViewModel 类型Doctor(实体),但是这一行:

return View(doctorViewModel);

实际上是将DoctorDetailsViewModel对象作为 ViewModel 传递。

尝试将您的 ViewModel 类型更改为DoctorDetailsViewModel

@model DoctorDetailsViewModel 
于 2013-08-15T06:41:18.680 回答