Afficher des données de deux tables dans une vue
Bonjour à tous,
Toute mes excuses pour le dérangement mais je rencontre quelques soucis pour l'affichage de données dans une vue avec des informations se trouvant dans deux tables d'une base de données.
J'ai deux tables :
° Patient : id_patient, nom_patient, prenom_patient
° Medicament : id_medicament, nom_medicament, date_prise_medicament, id_patient
J'ai une vue qui affiche la liste des médicaments pour un patient :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| @model IEnumerable<Nom_Application.Models.Medicament>
@{
ViewBag.Title = "Index";
}
<h2>Médicaments</h2>
@{string PatientID = ""; string PatientNom = ""; string PatientPrenom = "";}
@foreach (var item in Model){
PatientID = item.id_patient;
PatientNom = item.Patient.nom_patient;
PatientPrenom = item.Patient.prenom_patient;
}
<div>
<div class="form-horizontal">
<hr />
<fieldset class="fieldset fieldsetGrey">
<h4>Médicaments du patient :</h4>
<div class="form-group">
<div class="control-label col-md-2">
ID Patient
</div>
<div class="control-label col-md-4">
@Html.EditorFor(x => PatientID , new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", disabled = "disabled" } })
</div>
</div>
<div class="form-group">
<div class="control-label col-md-2">
Nom
</div>
<div class="col-md-4">
@Html.EditorFor(x => PatientNom, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", disabled = "disabled" } })
</div>
<div class="control-label col-md-2">
Prénom
</div>
<div class="col-md-4">
@Html.EditorFor(x => PatientPrenom, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", disabled = "disabled" } })
</div>
</div>
</fieldset>
</div>
</div>
<table class="table">
<tr>
<th>
Date
</th>
<th>
Médicament
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.date_prise_medicament)
</td>
<td>
@Html.DisplayFor(modelItem => item.nom_medicament)
</td>
</tr>
}
</table>
<div>
@Html.ActionLink("<< Retour", "Details", "Patients", new { id = id_patient}, null)
</div> |
Ainsi qu'un controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Nom_Application.Models;
namespace Nom_Application.Controllers
{
public class MedicinesController : Controller
{
private Nom_ApplicationEntities db = new Nom_ApplicationEntities();
// GET: Medicines
public ActionResult Index(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var medicaments = from m in db.Medicaments
where m.id_patient.Contains(id)
select m;
if (medicaments == null)
{
return HttpNotFound();
}
medicaments = medicaments.OrderByDescending(medic => medic.date_prise_medicament);
return View(medicaments);
} |
Les données des deux tables s'affichent bien sauf si la liste medicament est vide. Le pourquoi est évident à comprendre... mais c'est le comment corriger qui me pose des soucis. Pourriez-vous me donner quelques pistes ?
Un grand merci d'avance