1 pièce(s) jointe(s)
Récupérer des données d'une propriété de navigation qui est une collection
Bonjour,
J'ai créé une application de gestion de configurations Gestion_Configurations. La classe VM_Configuration comporte deux propriétés Postes_travail_config et Logiciels qui sont deux collections.
Au niveau du Controller ConfigurationController je ne parviens pas à récupérer les contenus des propriétés sonProcess, sonOs, sonEcran, pour chaque poste de travail récupéré pour une configuration donnée (voir imp ecran ci-jointe).
Merci beaucoup de me dire ce que j'ai besoin de rajouter dans les Include des méthodes ListerConfigurations et AfficherConfigurations pour récupérer les contenus de ces propriétés à l'écran.
Merci infiniment de votre aide!
Bien cordialement.
new_wave
ps: j'ai bien mis tous les using dans mon code mais les ai enlvés ici pour faciliter la lisibilité
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
|
namespace Gestion_Configurations.Models
{
[Table("Configuration")]
public class VM_Configuration
{
[Key]
public int Id_config { get; set; }
public string Nom_config { get; set; }
[Column("Id_service")]
[ForeignKey(nameof(sonService))]
public int Id_service { get; set; }
public VM_Service sonService { get; set; }
[Column("Id_script")]
[ForeignKey(nameof(sonScript))]
public int Id_script { get; set; }
public VM_Script sonScript { get; set; }
public List<VM_Poste_travail> Postes_travail_config { get; set; }
public List<VM_Logiciel> Logiciels { get; set; }
}
} |
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
|
namespace Gestion_Configurations.Models
{
public class VM_Poste_travail
{
[Key]
public int Id_poste_travail { get; set; }
public int Capacite_RAM { get; set; }
public string Unite_RAM { get; set; }
public int Capacite_Disque { get; set; }
public string Unite_capacite_disque { get; set; }
[Column("Id_processeur")]
[ForeignKey(nameof(sonProcess))]
public int Id_processeur { get; set; }
public VM_Processeur sonProcess { get; set; }
[Column("Id_OS")]
[ForeignKey(nameof(sonOS))]
public int Id_OS { get; set; }
public VM_Syst_Exploit sonOS { get; set; }
[Column("Id_ecran")]
[ForeignKey(nameof(sonEcran))]
public int Id_ecran { get; set; }
public VM_Ecran sonEcran { get; set; }
[Column("Id_config")]
[ForeignKey(nameof(saConfig))]
public int Id_config { get; set; }
public VM_Configuration saConfig { get; set; }
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
namespace Gestion_Configurations.Models
{
[Table("Logiciel")]
public class VM_Logiciel
{
[Key]
public int Id_logiciel { get; set; }
public string Nom_logiciel { get; set; }
public string Version_logiciel { get; set; }
public List<VM_Configuration> Liste_configs { get; set; }
}
} |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
namespace Gestion_Configurations.Controllers
{
public class ConfigurationController : Controller
{
E_gestconfigs_Context context = new E_gestconfigs_Context();
// GET: Configuration
/// <summary>
/// Affichage de toutes les configurations
/// </summary>
/// <returns></returns>
///
// GET: Configuration
[HttpGet]
public ActionResult ListerConfigurations()
{
try
{
IEnumerable<VM_Configuration> liste_configurations_pt_logiciels = new List<VM_Configuration>();
liste_configurations_pt_logiciels = context.Configurations.Include(config=>config.Postes_travail_config).Include(config=>config.Logiciels).Include(config => config.sonScript).Include(config => config.sonService).ToList();
if (liste_configurations_pt_logiciels != null)
{
return View("ListerConfigurations", liste_configurations_pt_logiciels);
}//fin de if
else
{
return HttpNotFound();
}//fin de else
}//fin de try
catch (Exception ex)
{
Console.WriteLine("une erreur s'est produite à l'affichage des configurations" + ex.Message);
return HttpNotFound();
}//fin de catch
}//fin de ListerConfigurations
/// <summary>
/// Affichage d'une configuration
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public ActionResult AfficherConfiguration(int id)
{
try
{
//on recherche la configuration dan le context
VM_Configuration configuration = new VM_Configuration();
configuration = context.Configurations.Include(config=>config.Postes_travail_config).Include(config=>config.Logiciels).Include(config=>config.sonService).Include(config=>config.sonScript).FirstOrDefault(config=>config.Id_config==id);
if (configuration != null)
{
return View("AfficherConfiguration", configuration);
}
else
{
return HttpNotFound();
}
}//fin de try
catch (Exception ex)
{
Console.WriteLine("une erreur s'est produite à l'affichage de la configurations d'id {0}", id, ex.Message);
return HttpNotFound();
}
}//fin de AfficherConfiguration |
Vue AfficherConfiguration.cshtml
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
|
@model Gestion_Configurations.Models.VM_Configuration
@{
ViewBag.Title = "AfficherConfiguration";
}
<h4>Afficher le détail d'une configuration </h4>
<div class="panel panel-success">
<div class="panel-heading">
<h5 class="panel-title">Détail d'une configuration</h5>
</div>
<div class="panel-body">
<label class="col-lg-2 control-label">Nom configuration </label>
<p>@Html.DisplayFor(Model => Model.Nom_config)</p>
<label class="col-lg-2 control-label">Nom service</label>
<p>@Html.DisplayFor(Model => Model.sonService.Nom_service)</p>
<label class="col-lg-2 control-label">Nom script</label>
<p>@Html.DisplayFor(Model => Model.sonScript.Nom_script)</p>
<label class="col-lg-2 control-label">Nombre postes de travail</label>
<p>@Html.DisplayFor(Model => Model.Postes_travail_config.Count)</p>
@foreach (var pt in Model.Postes_travail_config)
{
<label class="col-lg-2 control-label">Capacité Ram</label>
<p>@Html.DisplayFor(Model => pt.Capacite_RAM)</p>
<label class="col-lg-2 control-label">Unité RAM</label>
<p>@Html.DisplayFor(Model => pt.Unite_RAM)</p>
<label class="col-lg-2 control-label">Capacité Disque</label>
<p>@Html.DisplayFor(Model => pt.Capacite_Disque)</p>
<label class="col-lg-2 control-label">Unité capacité disque</label>
<p>@Html.DisplayFor(Model => pt.Unite_capacite_disque)</p>
<label class="col-lg-2 control-label">Processeur</label>
<p>@Html.DisplayFor(Model => pt.sonProcess.Nom_Processeur)</p>
<label class="col-lg-2 control-label">Systeme exploitation</label>
<p>@Html.DisplayFor(Model => pt.sonOS.Nom_OS)</p>
<label class="col-lg-2 control-label">Ecran</label>
<p>@Html.DisplayFor(Model => pt.sonEcran.Nom_ecran)</p>
<label class="col-lg-2 control-label">Configuration</label>
<p>@Html.DisplayFor(Model => pt.saConfig.Nom_config)</p>
}
</div>
</div> |