Malphai Malphai - 3 months ago 15
SQL Question

Display SQL Data List in MVC

so I get the error:


"foreach statement cannot operate on variables of type 'Project.Models.Contact' because 'Project.Models.Contact' does not contain a public definition for 'GetEnumerator'"




for some reason. I am trying to display as simple as possible my data rows in my database table to my application.

Model:

namespace Project.Models
{
public class Contact
{
public int Id { get; set; }
public string FirstName { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public int CompanyID { get; set; }
}
}


View:

@model List<Project.Contact>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Contact</h1>

<table>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Email</th>
<th>Phone</th>
</tr>
@foreach (var contact in Model)
{
<tr>
<td>@contact.FirstName</td>
<td>@contact.LastName</td>
<td>@contact.Email</td>
<td>@contact.Phone</td>
</tr>
}
</table>


Controller:

using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Mvc;

namespace Project.Controllers
{
public class HomeController : Controller
{
SqlConnection connectionString = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=Sublime;Integrated Security=True");

public ActionResult Index()
{
return View();
}

public ActionResult List()
{
connection.Open();
string query = "SELECT * FROM Contact";

var command = new SqlCommand(query, connection);
var dr = command.ExecuteReader();
var model = new List<Contact>();

while (dr.Read())
{
var contact = new Contact();

contact.Id = dr.GetInt32(0);
contact.FirstName = dr.GetString(1);
contact.Lastname = dr.GetString(2);
contact.Email = dr.GetString(3);
contact.Phone = dr.GetString(4);

model.Add(contact);
}

connection.Close();

return View(model);
}
}

Answer

As i explained in the comments you are passing the query to the View method). You should be pass the List of Contacts after executing the query

So make sure you are returning a list of Contact objects to the View method.

List<Contact> model = new List<Contact>();   
string query = "SELECT * FROM Contact";

using (var conn=new SqlConnection("Put Your conn string here"))
{
    using (var cmd = new SqlCommand(query,conn))
    {
        conn.Open();
        using (var rdr = cmd.ExecuteReader())
        {
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    var m = new Contact();
                    m.FirstName = rdr.GetString(rdr.GetOrdinal("FirstName"));
                    //map other properties here as well
                    model.Add(m);
                }
            }
        }
    }
}
return View(model);

Since you are sending a list of Contact objects to the view (and looping through them, you should make your view strongly typed to a list of Contact

@model List<Project.Contact>
<h1>Contact</h1>
<table>
    <tr>
        <th>FirstName</th>
        <th>LastName</th>
        <th>Email</th>
        <th>Phone</th>
    </tr>
    @foreach (var contact in Model)
    {
        <tr>
            <td>@contact.FirstName</td>
            <td>@contact.LastName</td>
            <td>@contact.Email</td>
            <td>@contact.Phone</td>
        </tr>
    }
</table>
Comments