0

I've just started learning ASP MVC. So far, i was using web forms, and i've got a lot of classes from web form web application. With GridViews that works fine, now i'am looking a solution how to present data from database in MVC. I've got a lot of classes like this one:

public class zmienneWczytywanieTresci
{
    public int druzynaID { get; set; }
    public string Druzyna { get; set; }
    public string LiczbaMeczy { get; set; }
    public string LiczbaGoliStrzelonych { get; set; }
    public string LiczbaGoliStraconych { get; set; }
    public string Zwyciestwa { get; set; }
    public string Remisy { get; set; }
    public string Porazki { get; set; }
    public string Punkty { get; set; }
 }

    public static List<zmienneWczytywanieTresci> wczytajTabele(string query, string grupa)
    {
        List<zmienneWczytywanieTresci> wczytajTabele = new List<zmienneWczytywanieTresci>();
        string CS = ConfigurationManager.ConnectionStrings["ligiConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            con.Open();
            SqlCommand com = new SqlCommand(query, con);
            com.Parameters.AddWithValue("@Grupa", grupa);
            SqlDataReader rdr = com.ExecuteReader();

            while (rdr.Read())
            {
                zmienneWczytywanieTresci infoTabela = new zmienneWczytywanieTresci();
                int druzynaID = Convert.ToInt32(rdr["Druzyna"]);
                infoTabela.Druzyna = wczytajNazweKlubu(druzynaID);
                infoTabela.LiczbaMeczy = rdr["LiczbaMeczy"].ToString();
                infoTabela.LiczbaGoliStrzelonych = rdr["LiczbaGoliStrzelonych"].ToString();
                infoTabela.LiczbaGoliStraconych = rdr["LiczbaGoliStraconych"].ToString();
                infoTabela.Zwyciestwa = rdr["Zwyciestwa"].ToString();
                infoTabela.Remisy = rdr["Remisy"].ToString();
                infoTabela.Porazki = rdr["Porazki"].ToString();
                infoTabela.Punkty = rdr["Punkty"].ToString();

                wczytajTabele.Add(infoTabela);
            }
            return wczytajTabele;
        }
    }

Is there option to present data from that list using foraech loop ?. I'am creaing a ViewBag with that lis.. and i dont know what to do now, I'can't find any solution. Many thanks for any advise !

1
  • why dont you use EF and linq with it\ Commented May 9, 2014 at 9:25

2 Answers 2

1

Example for your code.

Create a strongly typed view (by defining the type of the model) and play with it as you wanmt. This is a simple iteration for your example:

@model IEnumerable<zmienneWczytywanieTresci>

<ul>
@foreach (var myItem in Model)
{
    <li>@myItem.Druzyna</li>
    <li>@myItem.LiczbaMeczy</li>
    <li>@myItem.LiczbaGoliStrzelonych</li>
}
</ul>
Sign up to request clarification or add additional context in comments.

Comments

1

You would create a ViewModel (or the model you currently have zmienneWczytywanieTresci) which represent what you want to present on the screen, then just loop through it using foreach.

If you are new to mvc, I would suggest follow a tutorial, so you will think in mvc rather than webform.

I suggest mvc music store as tutorial: http://www.asp.net/mvc/tutorials/mvc-music-store

For your problem, look at part 5 of tutorial for code: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5

edit

If you are reading part 5 of tutorial straight away, the code which start with

@model IEnumerable<MvcMusicStore.Models.Album>

shows how you could loop through the list, in your case the

@model List<zmienneWczytywanieTresci>

will be the model, and rest is how you want to diplay the list

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.