WelGalaxy
WelGalaxy

Reputation: 27

List of Object in View Data , from Controller to View

Im a beginner in ASP.net Mvc. Im trying to print on my view a list of object coming from my controller.

This is my controller :

namespace PhoneTeleX.Controllers
{
public class SondageController : Controller
{
    Guid idSondage;
    bool termine;
    int TEC_ID;
    string TQ_Libelle;
    string lblEntrepriseCliente;

    public Guid IdSondage { get => idSondage; set => idSondage = value; }
    public bool Termine { get => termine; set => termine = value; }
    public int TEC_ID1 { get => TEC_ID; set => TEC_ID = value; }
    public string TQ_Libelle1 { get => TQ_Libelle; set => TQ_Libelle = value; }
    public string LblEntrepriseCliente { get => lblEntrepriseCliente; set => lblEntrepriseCliente = value; }

    public SondageController(Guid leID, bool leTermine, int leTEC, string leLibelle,string laEntreprise)
    {
        IdSondage = leID;
        Termine = leTermine;
        TEC_ID1 = leTEC;
        TQ_Libelle1 = leLibelle;
        lblEntrepriseCliente = laEntreprise;
    }

    public SondageController() { }

    public ActionResult SelectionSondage()
    {
        ViewData["Sondages"] = DAOSelectionSondage.refreshSondages();
        return View();
    }
}
}

This is my view

@{
ViewBag.Title = "SelectionSondage";
}

<html>
<head>
<meta charset="utf-8" />
<title>PhoneTeleX</title>

Recherche & Sélection de Sondages

<div>
    <input id="Text1" type="text" placeholder="Tapez ici le code ou nom du sondage désiré..." /> <button class="waves-effect waves-light btn" style="background-color: #008CBA; margin-left : 1.5%;" runat="server" OnClick="LogIn" Value="Rechercher">Rechercher</button>
</div>
<br />
<br />
<div>

    <!-- Affichage par table row de chaque Sondage-->
    <table>
     @foreach (SondageController myObject in ViewData["Sondage"])
    {
       @myObject.lblEntrepriseCliente; //this doesn't work, 
    }
    </table>
</div>

As you can see the method SelectionSondage from my Controller return a list of object i created from a SQLServerDatabase (im not using EntityFramework on this project). My final goal would be to print the content of my object as you can see in my view. That's why i 'd like to put into a ViewData my list of object to finaly display them on my view.

Can you help me ?

Upvotes: 1

Views: 1385

Answers (1)

WelGalaxy
WelGalaxy

Reputation: 27

Ok finnaly i had to do like this =>

My Controller :

public ActionResult SelectionSondage()
    {
        //ViewData["Sondages"] = DAOSelectionSondage.refreshSondages();
        ViewData["test"] = "bonjour ceci est un test";
        ViewData["listeSondages"] = DAOSelectionSondage.refreshSondages();
        return View("SelectionSondage");
    }

My view :

 @foreach (var SondageController in ViewData["listeSondages"] as List<PhoneTeleX.Controllers.SondageController>)
    {
        <p>@SondageController.LblEntrepriseCliente</p>
    }

Upvotes: 1

Related Questions