asp.net-mvc - 如何编辑 ViewModels 数据并将其保存回数据库

标签 asp.net-mvc entity-framework asp.net-mvc-4

我有一个 ViewModel,它由三个实体连接起来,以将所有实体的数据获取到一个 View 表单中。虽然我成功地实现了同样的目标。但我不知道如何编辑数据并将数据保存回数据库。我的模型类通过一对一的关系连接起来。

我的模型是:

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

我的 View 模型是:

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

    }

我的 Controller 是:

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

我的 Controller 类是:

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

请帮我看看我该怎么办。提前致谢。

最佳答案

在这里,用于创建:

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

    return View(doctor);
}

关于asp.net-mvc - 如何编辑 ViewModels 数据并将其保存回数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18237945/

相关文章:

linq - linq 为存储过程的两次不同执行返回相同的数据?

c# - 如何从 EF 的非异步 SaveChanges 安全地调用异步方法?

javascript - MVC : Toggle checkbox visibility in modal using JQuery

C# Entity Framework - 如何获取指定为参数的列

asp.net-mvc - 当我们在 Visual Studio 中进行 FTP 发布时,将为 Controller 创建哪个 dll

c# - 如何在用户通过身份验证后将变量传递给每个 Controller

.net - 从 Dot Net MVC 4 异步执行批处理脚本

c# - Controller 收不到来自json的数据

asp.net-mvc - MVC模型验证程序化注册支持

c# 使用 linq 将 viewModel List 对象上的字符串属性串联起来显示在 MVC View 上