Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在ASP.NET MVC4中保存和编辑视图模型_Asp.net Mvc_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 在ASP.NET MVC4中保存和编辑视图模型

Asp.net mvc 在ASP.NET MVC4中保存和编辑视图模型,asp.net-mvc,entity-framework,asp.net-mvc-4,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; } pub

我有一个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>        
 }
@model HospitalManagementSystem.Models.Doctor
@{
ViewBag.Title=“创建”;
}
医生
名称
@EditorFor(model=>model.Name)
特长
@EditorFor(model=>model.Speciality)
加入日期
@EditorFor(model=>model.DateOfJoinig)
@Html.HiddenFor(model=>model.DoctorId)
地址
@EditorFor(model=>model.DoctorAddress.Address)
地区
@EditorFor(model=>model.DoctorAddress.Area)
城市
@EditorFor(model=>model.DoctorAddress.City)
@Html.HiddenFor(model=>model.DoctorId)
门诊费
@EditorFor(model=>model.DoctorCharge.OPDCharge)
光电电荷
@EditorFor(model=>model.DoctorCharge.OPDRevisitCharge)

}

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

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

此行:

@model HospitalManagementSystem.Models.Doctor
声明视图需要类型为
Doctor
(实体)的ViewModel,但此行:

return View(doctorViewModel);
实际上正在将
DoctorDetailsViewModel
对象作为ViewModel传递

尝试将ViewModel类型更改为
DoctorDetailsViewModel

@model DoctorDetailsViewModel 

我在保存时收到错误消息:“传递到字典中的模型项的类型为'HM.ViewModels.DoctorDetailsViewModel',但此字典需要'HM.Models.Doctor'类型的模型项。我尝试更改了您告诉我的模型项,但当我单击“创建”时,数据未保存并重定向到同一页面。没有错误的迹象!可能是您的
ModelState.IsValid
为false。