1

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 ?

3
  • 1
    What does DAOSelectionSondage.refreshSondages returns ? Commented Jun 20, 2018 at 0:29
  • Also when you say this doesn't work,, What does that mean ? Are you getting any error message ? If any, what are those ? Commented Jun 20, 2018 at 0:39
  • @Shyju DAOSelectionSondage.refreshSondages, this method return a list of object i created from my DataBase, When i say 'this doesn't work' its because it's supposed to word like that , but it doesnt Commented Jun 20, 2018 at 11:05

1 Answer 1

1

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>
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.