I'm putting together a fairly simple Code-First MVC5 Inventory Tracking application. I've gotten my app to Seed() and all of my Maintenance tables (Locations, Vendors, Statuses, etc.) I can view/create/edit/delete. I'm now working on the View for my main [INV_Assets] model, but when trying to foreach through the items in my model to display all [INV_Assets] in a table, I get the error: foreach statement cannot operate on variables of type 'Tracker.Models.INV_Assets' because 'Tracker.Models.INV_Assets' does not contain a public definition for 'GetEnumerator'.

Below is the Model for my [INV_Assets]:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using GridMvc.DataAnnotations;
using System.Web.Mvc;
using Tracker.Models;

namespace Tracker.Models
{
    [GridTable(PagingEnabled = true, PageSize = 30)]
    public class INV_Assets 
    {
        // Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model.


        public int Id { get; set; }

        public int Model_Id { get; set; }
        [ForeignKey("Model_Id")]
        public virtual INV_Models Model { get; set; }

        [Required]
        public int Manufacturer_Id { get; set; }
        [ForeignKey("Manufacturer_Id")]
        public virtual INV_Manufacturers Manufacturer { get; set; }

        [Required]
        public int Type_Id { get; set; }
        [ForeignKey("Type_Id")]
        public virtual INV_Types Type { get; set; }

        [Required]
        public int Location_Id { get; set; }
        [ForeignKey("Location_Id")]
        public virtual INV_Locations Location { get; set; }

        public int Vendor_Id { get; set; }
        [ForeignKey("Vendor_Id")]
        public virtual INV_Vendors Vendor { get; set; }

        [Required]
        public int Status_Id { get; set; }
        [ForeignKey("Status_Id")]
        public virtual INV_Statuses Status { get; set; }

        public string ip_address { get; set; }

        public string mac_address { get; set; }

        public string note { get; set; }
        public string owner { get; set; }
        public decimal cost { get; set; }
        public string po_number { get; set; }
        public string description { get; set; }

        public int invoice_number{ get; set; }

        [Required]
        public string serial_number { get; set; }

        [Required]
        public string asset_tag_number { get; set; }

        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime? acquired_date { get; set; }

        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime? disposed_date { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime created_date { get; set; }

        [Required]
        public string created_by { get; set; }

        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime modified_date { get; set; }

        public string modified_by { get; set; }

        // Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.)
        //public bool available { get; set; }
    }
}

[INV_Assets] View:

@using GridMvc.Html
@model  Tracker.Models.INV_Assets

@{
    ViewBag.Title = "Home Page";
}
<table style="width:100%;">

@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Status)</td>
        <td>@Html.DisplayFor(modelItem => item.Location)</td>
        <td>@Html.DisplayFor(modelItem => item.owner)</td>
        <td>@Html.DisplayFor(modelItem => item.Type)</td>
        <td>@Html.DisplayFor(modelItem => item.Manufacturer)</td>
        <td>@Html.DisplayFor(modelItem => item.Model)</td>
        <td>@Html.DisplayFor(modelItem => item.Vendor)</td>
        <td>@Html.DisplayFor(modelItem => item.description)</td>
        <td>@Html.DisplayFor(modelItem => item.asset_tag_number)</td>
        <td>@Html.DisplayFor(modelItem => item.serial_number)</td>
        <td>@Html.DisplayFor(modelItem => item.ip_address)</td>
        <td>@Html.DisplayFor(modelItem => item.mac_address)</td>
        <td>@Html.DisplayFor(modelItem => item.po_number)</td>
        <td>@Html.DisplayFor(modelItem => item.invoice_number)</td>
        <td>@Html.DisplayFor(modelItem => item.cost)</td>
        <td>@Html.DisplayFor(modelItem => item.note)</td>
        <td>@Html.DisplayFor(modelItem => item.acquired_date)</td>
        <td>@Html.DisplayFor(modelItem => item.disposed_date)</td>
        <td>@Html.DisplayFor(modelItem => item.created_date)</td>
        <td>@Html.DisplayFor(modelItem => item.created_by)</td>
        <td>@Html.DisplayFor(modelItem => item.modified_date)</td>
        <td>@Html.DisplayFor(modelItem => item.modified_by)</td>
    </tr>
}

</table>

I'm not quite sure how to get around this error and wondered if someone with more experience could weigh-in?

I am getting no issue (not sure why) from my [INV_Locations] Model/View when doing the same:

[INV_Locations]:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Tracker.Models
{
    public class INV_Locations
    {
        public int Id { get; set; }
        public string location_dept { get; set; }
        public string location_room { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime created_date { get; set; }

        [Required]
        public string created_by { get; set; }

        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime modified_date { get; set; }

        public string modified_by { get; set; }
    }
}

[INV_Locations] View:

@model IEnumerable<Tracker.Models.INV_Locations>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h3>Maintenance - Locations</h3>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th></th>
        <th>
            @*@Html.DisplayNameFor(model => model.location_dept)*@
            Dept:
        </th>
        <th>
            Room:
        </th>
        <th>
            Created:
        </th>
        <th>
            By:
        </th>
        <th>
            Modified:
        </th>
        <th>
            By:
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.location_dept)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.location_room)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.created_date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.created_by)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.modified_date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.modified_by)
        </td>
    </tr>
}

</table>

I tried changing @model Tracker.Models.INV_Assets to @model IEnumerable<Tracker.Models.INV_Assets> on my [INV_Assets] View (just like how my [INV_Locations] View has it. This allows my solution to Build, but when trying to run my application to that View I receive: System.InvalidOperationException The model item passed into the dictionary is of type 'Tracker.Models.INV_Assets', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[Tracker.Models.INV_Assets]'.


EDIT - Regarding Shyju's Suggestion:

I changed my HomeController Index() from:

    TrackerContext _db = new TrackerContext();
    public ActionResult Index(INV_Assets defModel)
    {
        return View(defModel);
    }

to:

    TrackerContext _db = new TrackerContext();
    public ActionResult Index(INV_Assets defModel)
    {
        var assetList = new List<Tracker.Models.INV_Assets>();

        assetList.Add(defModel);
        return View(assetList);
    }

While my View now loads, all I get for the Table HTML is:

<table style="width:100%;">

    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td>0</td>
        <td>0.00</td>
        <td></td>
        <td></td>
        <td></td>
        <td>01/01/0001</td>
        <td></td>
        <td>01/01/0001</td>
        <td></td>
    </tr>

</table>

I've confirmed that my DBContext has 2 complete [INV_Assets] records in it, but they are not showing up in the View? How do I correctly load all the instances of my [INV_Assets] model into the new assetList variable?


EDIT2:

Modified my Index() to:

    TrackerContext _db = new TrackerContext();
    public ActionResult Index(INV_Assets defModel)
    {
        var assetList = _db.INV_Assets.ToList(); // EXCEPTION
        return View(assetList);
    }

but now, while the solution builds, the application fails when I try to run it. Details below:

Error: An exception of type 'System.Data.DataException' occurred in EntityFramework.dll but was not handled in user code. Additional information: An exception occurred while initializing the database. See the InnerException for details.

Message: An exception occurred while initializing the database. See the InnerException for details.

InnerException: {"The underlying provider failed on Open."}

Source: EntityFramework

Related posts

Recent Viewed