Variables from one table display along side another in the view

Hi there I was looking for a way to select certain fields from the different database tables and output it into one table in the view.This is the code I have so far although it is only able to retrieve information from the patients table. Any help would be greatly appreciated thanks.

model

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

Dont think this is right.

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

view

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

Controller

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

Answers


Rather than return the data directly from the database to the View you should create a View-Model in the controller action.

This ViewModel would contain all of the information that you need to display.

Using a ViewModel ensures that only the data required for the View is returned. It means rendering your View is more simple and you can use data from many different sources in one single ViewModel.


Need Your Help

Calculate with the Scanner Method in Java

java methods input

I'm trying to make a calculator where it's possible to either take in a single number, or take in a calculation (f.ex 2+2). Is it even possible to take in a calculation with scanner, or do some of ...

Export Gridview to Excel: Cannot export the correct data displayed

c# .net export-to-excel

I need to export my gridview data to excel. No problems for this.