视图中一个表中的变量与另一个表并排显示

本文关键字:另一个 显示 变量 一个 视图 | 更新日期: 2023-09-27 18:06:48

你好,我正在寻找一种方法从不同的数据库表中选择某些字段,并将其输出到视图中的一个表中。这是到目前为止我所拥有的代码,尽管它只能从patients表中检索信息。如有任何帮助,不胜感激。

模型
namespace ChampPMS.Models
{
    public class Patient
    {
        public int PatientID { get; set; }     
        public string HouseChartNo { get; set; }    
        public string ClinicChartNo { get; set; }  
        public string Title { get; set; }  
        public string FirstName { get; set; }   
        public DateTime DOB { get; set; }

我不认为这是对的。

       //Other Table variables
        public Admission  ExpectedDate { get; set; } ------->from bed 
        public Beds Bed { get; set; }  ------->from admissions

}

    public class Beds
    {
        public int BedID { get; set; }
        public int RoomID { get; set; }
        public string Bed { get; set; }         
    }
    public class Admission
    {
        public int AdmissionID { get; set; }
        public int PatientID { get; set; }           
        public DateTime ExpectedDate { get; set; }       
        public int BedID { get; set; }
        public string Phone { get; set; }        
    }
        public PatientDBContext()
        : base("PatientsDBContext")//connection string
    {
    }
    public DbSet<Admission> Admission { get; set; }
    public DbSet<Beds> Beds { get; set; }
    public DbSet<Patient> Patient { get; set; }
}
视图

@model IEnumerable<ChampPMS.Models.Patient>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create", null, new { @class = "btn btn-success" })
</p>
<table class="table  table-condensed table-striped table-hover table-bordered ">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.HouseChartNo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ClinicChartNo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DOB)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Bed)----->from bed table
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ExpectedDate)----->from admission table
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.HouseChartNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ClinicChartNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SurName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DOB)
            </td>
            <td>
                @Html.DisplayNameFor(model => model.Bed)------->from bed table
            </td>
            <td>
                @Html.DisplayNameFor(model => model.ExpectedDate) ----->from admission table
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.PatientID }, new { @class = "btn btn-xs btn-info" }) |
                @Html.ActionLink("Details", "Details", new { id = item.PatientID }, new { @class = "btn btn-xs btn-primary" }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.PatientID }, new { @class = "btn btn-xs btn-danger" })
            </td>
        </tr>
    }
</table>
控制器

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ChampPMS.Models;
namespace ChampPMS.Controllers
{
    public class PatientsController : Controller
    {
        private PatientDBContext db = new PatientDBContext();
        // GET: Patients
        public ActionResult Index()
        {
            return View(db.Patient.ToList());
        }
        // GET: Patients/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Patient patient = db.Patient.Find(id);
            if (patient == null)
            {
                return HttpNotFound();
            }
            return View(patient);
        }
        // GET: Patients/Create
        public ActionResult Create()
        {
            return View();
        }
        // POST: Patients/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "PatientID,HouseChartNo,ClinicChartNo,Title,FirstName,SurName,DOB,HouseName,Street,Town,County,Telephone,Mobile,Gender,Occupation,Marital,TaxNumber,GMSnumber,DPSnumber,ReligionID,Status,HashCode")] Patient patient)
        {
            if (ModelState.IsValid)
            {
                db.Patient.Add(patient);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(patient);
        }
        // GET: Patients/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Patient patient = db.Patient.Find(id);
            if (patient == null)
            {
                return HttpNotFound();
            }
            return View(patient);
        }
        // POST: Patients/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "PatientID,HouseChartNo,ClinicChartNo,Title,FirstName,SurName,DOB,HouseName,Street,Town,County,Telephone,Mobile,Gender,Occupation,Marital,TaxNumber,GMSnumber,DPSnumber,ReligionID,Status,HashCode")] Patient patient)
        {
            if (ModelState.IsValid)
            {
                db.Entry(patient).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(patient);
        }
        // GET: Patients/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Patient patient = db.Patient.Find(id);
            if (patient == null)
            {
                return HttpNotFound();
            }
            return View(patient);
        }
        // POST: Patients/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Patient patient = db.Patient.Find(id);
            db.Patient.Remove(patient);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

视图中一个表中的变量与另一个表并排显示

与其直接从数据库返回数据给视图,不如在控制器动作中创建一个视图模型。

这个ViewModel将包含您需要显示的所有信息。

使用ViewModel确保只返回视图所需的数据。这意味着渲染视图更简单,你可以在一个ViewModel中使用来自许多不同来源的数据。

相关文章: