如何按病房分隔患者

本文关键字:患者 分隔 病房 何按 | 更新日期: 2023-09-27 18:29:44

我遇到了大约两个小时的问题,我试图根据点击的病房显示患者。我设法让它显示病人,但它显示所有病人,而不是病房特有的病人。如何将患者分到特定的病房?

这是我的cs模型,它包含Ward和Patient类,以及我执行的种子和我正在使用DbContext。

    using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MvcHospital.Models
{
    //Make classes 
    //Make DbSets 
    //Make Controllers
    //Run Program - To Build
    //Make Views
    //Change Connection name in config

    //Do seeding after the above
    public class WardSeed: DropCreateDatabaseAlways<DbWard>
    {
        protected override void Seed(DbWard context)
        {
            context.Wards.Add(new Ward()
            {
                WardTitle = "Ward A",
                WardCreation = "21/1/2014",
                Patient =
                    new List<Patient>()
            {
                new Patient() { PatientName = "Sarah Connors", PatientGender="F"}
            }
            });
            context.Wards.Add(new Ward()
                {
                    WardTitle = "Ward B",
                    WardCreation = "28/5/2014",
                    Patient =
                    new List<Patient>()
                    {
                        new Patient() { PatientName = "Frank Rogers", PatientGender="M"},
                        new Patient() { PatientName = "Tom Jackson", PatientGender="M"}
                    }
                });
            context.SaveChanges();
            base.Seed(context);
        }
        }
    public class DbWard : DbContext
    {
        public DbSet<Ward> Wards { get; set; }
        public DbSet<Patient> Patients { get; set; }
        public DbWard()
            : base("WardConnString")
        {
        }
    }
    public class Patient
    {
        public int PatientId { get; set; }
        public string PatientName { get; set; }
        public string PatientGender { get; set; }
        public Ward Ward { get; set; }
    }
    public class Ward
    {
        public int WardId { get; set; }
        public string WardTitle { get; set; }
        public string WardCreation { get; set; }
        public List<Patient> Patient { get; set; }
    }
}

这是我试图用来显示详细信息的带有注释的代码的控制器。

using MvcHospital.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcHospital.Controllers
{
    public class HomeController : Controller
    {
        private DbWard db = new DbWard();
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View(db.Wards.ToList());
        }
        //
        // GET: /Home/Details/5
        public ActionResult Details(int id)
        {
            var wrd = db.Patients.ToList();
           // return View(db.Wards.Include("Patient").SingleOrDefault(item => item.WardId == id));
            //var wrd = db.Patients.Find(id);
            if (wrd == null)
            {
                return View();
            }
            else
                return View(wrd);
        }
    }
}

详细信息视图页面,我可以在其中查看患者,但不幸的是,我看到的是所有患者,而不是只属于一个病房的患者。

 @model IEnumerable<MvcHospital.Models.Patient>
@{
    ViewBag.Title = "Details";
}
<h2>Details</h2>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.PatientName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PatientGender)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PatientName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PatientGender)
        </td>
        <td>
            @Html.ActionLink("Home", "Index") 
        </td>
    </tr>
}
</table>

如何按病房分隔患者

在控制器的Details()操作中,您可以从数据库中查询所有患者,而不仅仅是属于所选病房的患者。

我假设您传入的id参数是选定的病房id。您可能需要将病房id添加到Patient类中,并将其设置为外键:

public class Patient
{
    public int PatientId { get; set; }
    public string PatientName { get; set; }
    public string PatientGender { get; set; }
    public int WardId { get; set; }    //Add this
    public Ward Ward { get; set; }
}

患者查询:

var patient = db.Patients.Where(p => p.WardId == id).ToList();