title: ASP.NET Razor - How to display data from multiple tables in database
body: <p>Sorry if the question isn't worded well but I am very confused and it is very late but I will do my best to explain. So I am trying to display all of the orders from my database into a table without using the Entity Framework. the problem is when I go to orders/getAllOrders I just get a big error that says "the view" then a bunch of </p>
  
  <pre><code> &lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Javier&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Groovestring&lt;/td&gt;&lt;td&gt;3.950000&lt;/td&gt;&lt;/tr&gt;etc..
  </code></pre>
  
  <p>with my data in it but I don't know how to display it properly. I know my query is working fine and my data is being retrieved fine but I am stumped on displaying it</p>
  
  <p>This what I currently have for my controller.</p>
  
  <pre><code>namespace Website.Controllers
  {
  public class OrderController : Controller
  {
  
      private static bool CanConnectToDb()
      {
          var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
          try
          {
              cnn.Open();
              return true;
          }
          catch (Exception exception)
          {
              return false;
          }
          finally
          {
              if (cnn.State != ConnectionState.Closed)
                  cnn.Close();
          }
      }
  
  
      public ActionResult getAllOrders()
      {
          // Connect to Database
          var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
          cnn.Open();
          // Create Command
          SqlCommand cmd = new SqlCommand(
              @"SELECT CustomerOrderId, FirstName ,ProductId, [Name], Price
                  FROM OrderProduct AS o
                  INNER JOIN CustomerOrder co ON co.id = o.CustomerOrderId
                  INNER JOIN Product p ON p.Id = o.ProductId
                  INNER JOIN Customer c ON c.Id = co.CustomerId
                  ORDER BY ProductId"
              );
  
          cmd.Connection = cnn;
          string store = "";
          // Read from Database
          SqlDataReader reader = cmd.ExecuteReader();
  
          var orders = new OrderIndexViewModel();
          while (reader.Read())
          {
              orders.CustomerOrderId = reader.GetInt32(0);
              orders.FirstName = reader.GetString(1);
              orders.ProductId = reader.GetInt32(2);
              orders.Name = reader.GetString(3);
              orders.Price = reader.GetDecimal(4);
              store += "&lt;tr&gt;&lt;td&gt;" + orders.CustomerOrderId + "&lt;/td&gt;&lt;td&gt;" + orders.FirstName + "&lt;/td&gt;&lt;td&gt;" + orders.ProductId + "&lt;/td&gt;&lt;td&gt;" + orders.Name + "&lt;/td&gt;&lt;td&gt;" + orders.Price + "&lt;/td&gt;&lt;/tr&gt;";
          }
          cnn.Close();
          return View(store);
      }
    }
  }
  </code></pre>
  
  <p>EDIT: When I change the return to </p>
  
  <pre><code>rerturn View(orders);
  </code></pre>
  
  <p>I get this error</p>
  
  <pre><code>The model item passed into the dictionary is of type 'Website.Models.OrderIndexViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Website.Models.OrderIndexViewModel]'.
  </code></pre>
  
  <p>This is my Model</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace Website.Models
  {
      public class OrderIndexViewModel
      {
          public int CustomerOrderId { get; set; }
          public string FirstName { get; set; }
          public int ProductId { get; set; }
          public string Name { get; set; }
          public decimal Price { get; set; }
      }
  }
  </code></pre>
  
  <p>And then finally this is my view</p>
  
  <pre><code>@model IEnumerable&lt;Website.Models.OrderIndexViewModel&gt;  
  @{
      ViewBag.Title = "getAllOrders";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h2&gt;getAllOrders&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table class="table"&gt;
      &lt;tr&gt;
          &lt;th&gt;
              Customer Oder Id
          &lt;/th&gt;
          &lt;th&gt;
              First Name
          &lt;/th&gt;
          &lt;th&gt;
              ProductId
          &lt;/th&gt;
          &lt;th&gt;
              Name
          &lt;/th&gt;
          &lt;th&gt;
              Price
          &lt;/th&gt;
      &lt;/tr&gt;
      @foreach (var item in Model)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.CustomerOrderId)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.FirstName)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.ProductId)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Name)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Price)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                  @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                  @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  &lt;/table&gt;
  </code></pre>
  
  <p>Any help will be thoroughly appreciated</p>