在ASP.NET MVC4中保存和编辑ViewModel

时间:2013-08-15 06:24:24

标签: 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 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; }
}

我的ViewModel是:

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的模型项”

请帮帮我怎么办。提前谢谢。

1 个答案:

答案 0 :(得分:0)

这一行:

@model HospitalManagementSystem.Models.Doctor

声称View期望一个类型为Doctor(实体)的ViewModel,但是这一行:

return View(doctorViewModel);

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

尝试将ViewModel类型更改为DoctorDetailsViewModel

@model DoctorDetailsViewModel