title: Display List return from a controller to a table in view ASP.NET MVC4
body: <p>I have a function in my controller class, it return a list of data, I want to display it in table structure in my view page.
  i have tried the following code but it shows some error</p>
  
  <blockquote>
    <p>"Class does not contain definition of GetEnumerator"</p>
  </blockquote>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public ActionResult data(Message msg,IEnumerable&lt;sample&gt; dept)
  {
      dbconnection db = new dbconnection();
      sample s = new sample();
      SqlConnection con = new SqlConnection(db.GetconString());
      DataTable dt;   
      List&lt;examplemvc1.Models.sample&gt; datatable = new List&lt;sample&gt;();
      dt = db.BuildDT("select * from MVCsample");
      foreach (DataRow row in dt.Rows)
      {
          s.FirstName = Convert.ToString(row["First Name"]);
          s.LastName = Convert.ToString(row["Last Name"]);
          s.Address = Convert.ToString(row["Address"]);
          s.PhoneNumber = Convert.ToString(row["PhoneNumber"]);
          s.Location = Convert.ToString(row["Location"]);
          datatable.Add(s);
          dept = datatable;  
      }
      ViewBag.tabledata = dept;
      return View(dept) ;
  }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public class sample
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Address { get; set; }
      public string PhoneNumber { get; set; }
      public string Location { get; set; }
      public string tabledata { get; set; }
  }
  
  public class Message
  {
      public IEnumerable&lt;sample&gt; sampleList { get; set; }
      public string MessageText { get; set; }
      public string MessageFrom { get; set; }
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model examplemvc1.Models.sample
  
  @foreach (var data in Model)
  {
      &lt;table&gt;&lt;tr&gt;&lt;td&gt;@data.FirstName&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  this is how my entire view looks like </p>
  
  <pre><code>@model List&lt;examplemvc1.Models.sample&gt;
  @{
      ViewBag.Title = "Registration Form";
  }
  &lt;head&gt;
      &lt;script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js"&gt;&lt;/script&gt;
      &lt;link href="../../Style/sample.css" rel="stylesheet" type="text/css" /&gt;
      &lt;script src="../../Scripts/samplescript.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;h2&gt;
      Registration Form &lt;/h2&gt;
     &lt;body&gt;
  
    &lt;table&gt;
   &lt;tr&gt;
   &lt;th&gt;
     First Name
   &lt;/th&gt;
   &lt;/tr&gt;
  
  @foreach (var data in Model)
  {
      &lt;tr&gt;&lt;td&gt;@data.FirstName&lt;/td&gt;&lt;/tr&gt;
  }
  &lt;/table&gt;
  
  
  
  @using (Html.BeginForm())
  {
  
      &lt;table id="table1"&gt;
      &lt;tr&gt;
           &lt;td&gt; 
  
               @Html.Label("Enter FirstName", new { @class = "standard_label_style" })
          &lt;/td&gt;
           &lt;td&gt;
               @Html.TextBoxFor(a =&gt; a.FirstName, new { @class = "class1", title = "Enter FirstName", id = "NameBox", placeholder = "Enter name", onkeydown = "return TextField(event)" })
            &lt;span class="errorMessage"&gt;&lt;/span&gt;
             @if (!ViewData.ModelState.IsValid)
             {       
                  &lt;span class="field-validation-error"&gt;
                  @ViewData.ModelState["FirstName"].Errors[0].ErrorMessage&lt;/span&gt;
             }     
      &lt;/td&gt;
      &lt;/tr&gt;
  
  
      &lt;tr&gt;
      &lt;td&gt;
      @Html.Label("Enter LastName", new { @class = "standard_label_style" })
      &lt;/td&gt;
      &lt;td&gt;
  
      @Html.TextBoxFor(a =&gt; a.LastName, new { @class = "class1", placeholder = "Enter name", id = "LastNameBox", title = "Enter Lastname", onkeydown = "return TextField(event); " })
          &lt;span class="errorMessage"&gt;&lt;/span&gt;
         &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
      &lt;td&gt;
       @Html.Label("Enter Address", new { @class = "standard_label_style" })
       &lt;/td&gt;
       &lt;td&gt;
        @Html.TextBoxFor(a =&gt; a.Address, new { @class = "class1", id = "AddressBox", placeholder = "Enter name", title = "Enter Address" })
      &lt;span class="errorMessage"&gt;&lt;/span&gt;
     &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
     &lt;td&gt;
      @Html.Label("Enter PhoneNumber", new { @class = "standard_label_style" })
      &lt;/td&gt;
      &lt;td&gt;
      @Html.TextBoxFor(a =&gt; a.PhoneNumber, new { @class = "class1", id = "PhoneBox", placeholder = "Enter name", title = "Enter Phonenumber", onkeydown = "return ValidateNumber(event);" })
      &lt;span class="errorMessage"&gt;&lt;/span&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;
      @Html.Label("Enter Location", new { @class = "standard_label_style" })
      &lt;/td&gt;&lt;td&gt;
        @Html.TextBoxFor(a =&gt; a.Location, new { @class = "class1", id = "LocationBox", placeholder = "Enter name", title = "Enter Location" })
        &lt;span class="errorMessage"&gt;&lt;/span&gt;
        &lt;/td&gt;
       &lt;/tr&gt;
     &lt;/table&gt;
      &lt;input type="button" id="btnSave"  value="Register"/&gt;
       &lt;input type="button" value="Clear"/&gt;
       &lt;input type="button" id="Tabledata" value="Tabledata"/&gt;
      &lt;div id="div1"&gt;&lt;/div&gt;
  
     @*@Html.ValidationSummary()*@
  
  
  
  
  
  
  
  
     &lt;script type="text/javascript"&gt;
  
         function ValidateNumber(e) {
             var evt = (e) ? e : window.event;
             var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
             if (charCode &gt; 31 &amp;&amp; (charCode &lt; 48 || charCode &gt; 57)) {
                 return false;
             }
             return true;
         };
         function TextField(e) {
             var evt = (e) ? e : window.event;
             var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
             if (charCode &gt; 31 &amp;&amp; (charCode &lt; 48 || charCode &gt; 56)) {
                 return true;
             }
             else if (charCode == 8 || charCode == 9) {
                 return true;
             }
             return false
  
         };
  
  
     &lt;/script&gt;
  
  }
     &lt;/body&gt;
  </code></pre>
  
  <p>Error is shown in my view code, actually I don't know how to get that values in view page. I am newbie in mvc. Please help me to solve this. Any help will be greatly appreciated. </p>
  
  <p><strong>"Update"</strong></p>
  
  <p><strong>I have solved My problem with the help of Stackoverflow Below is my correctcode</strong></p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code> public ActionResult data()
          {
              SomeViewModel model = new SomeViewModel();
              dbconnection db = new dbconnection();
              SqlConnection con = new SqlConnection(db.GetconString());
              DataTable dt = db.BuildDT("select * from MVCsample");
              foreach (DataRow row in dt.Rows)
              {
                  sample s = new sample();
                  s.FirstName = Convert.ToString(row["First Name"]);
                  s.LastName = Convert.ToString(row["Last Name"]);
                  s.Address = Convert.ToString(row["Address"]);
                  s.PhoneNumber = Convert.ToString(row["PhoneNumber"]);
                  s.Location = Convert.ToString(row["Location"]);
                  model.samples.Add(s);
              }
              return View(model);
          }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>namespace examplemvc1.Models
  {
      public class sample
      {
  
          public string FirstName { get; set; }
  
          public string LastName { get; set; }
  
          public string Address { get; set; }
  
  
          public string PhoneNumber { get; set; }
  
          public string Location { get; set; }
           public string tabledata { get; set; }
  
      }
      public class Message
      {
          public IEnumerable&lt;sample&gt; sampleList { get; set; }
          public string MessageText { get; set; }
          public string MessageFrom { get; set; }
      }
      public class SomeViewModel
      {
          public SomeViewModel()
          {
              samples = new List&lt;sample&gt;();
              sample = new sample();
          }
          public List&lt;sample&gt; samples { get; set; }
          public sample sample { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>view</strong></p>
  
  <pre><code>@model examplemvc1.Models.SomeViewModel
  </code></pre>
  
  <p>//................
        </p>
  
  <pre><code>@foreach (var data in Model.samples)
  {
      &lt;tr&gt;&lt;td&gt;@data.FirstName&lt;/td&gt;
       &lt;td&gt;@data.LastName&lt;/td&gt;
       &lt;td&gt;@data.Address&lt;/td&gt;
       &lt;td&gt;@data.PhoneNumber&lt;/td&gt;
       &lt;td&gt;@data.Location&lt;/td&gt;&lt;/tr&gt;
  }
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>Just look at Stephen Muecke's Answer below he has explained the context very clearly</strong></p>