我如何按病房分开患者?

时间:2014-11-09 23:48:01

标签: c# asp.net-mvc entity-framework

我有两个小时的问题,我正在尝试显示患者,具体取决于哪个病房点击。我设法将它显示给显示患者,但它显示所有患者而不是特定于病房的患者。 如何将患者分开到特定病房?

这是我的模型cs,它包含病房和病人类,我执行的播种和 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; }
    }
}

这是带有注释掉代码的Controller,我试图用它来显示详细信息。

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>

1 个答案:

答案 0 :(得分:2)

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

我假设您传入的id参数是选中的病房ID。您可能希望将WardId添加到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();