0
votes

I've looked at numerous examples of this error online. But I don't know enough to apply them. I'm using EF 6.1.2 and Visual Studio 2012. Everything is in one solution. I tried a separate class library for the Entity Model but it did not work. So i thought i'd simplify things but it still doesn't work.

Let me know if you need anything else. Thanks!

This is my controller file: I added everything in the file just to be on the safe side. the first action method is the only one i use.

CustomerController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClaimsManagement2.Models;

namespace ClaimsManagement2.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/
        public ActionResult Index()
        {
        ClaimsManagementContext customerContext = new ClaimsManagementContext()  
        IEnumerable<Customer> customer = customerContext.Customers.ToList();

        return View(customer);
    }

    //
    // GET: /Customer/Details/5

    public ActionResult Details(int id)
    {
        return View();
    }

    //
    // GET: /Customer/Create

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

    //
    // POST: /Customer/Create

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    //
    // GET: /Customer/Edit/5

    public ActionResult Edit(int id)
    {
        return View();
    }

    //
    // POST: /Customer/Edit/5

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add update logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    //
    // GET: /Customer/Delete/5

    public ActionResult Delete(int id)
    {
        return View();
    }

    //
    // POST: /Customer/Delete/5

    [HttpPost]
    public ActionResult Delete(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add delete logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
        }
    }
}

Index.cshtml file

@model IEnumerable<ClaimsManagement2.Models.Customer.>

@{
    ViewBag.Title = "Index";
 }

<h2>Index</h2>

<p>
     @Html.ActionLink("Create New", "Create")
 </p>
 <table>
 <tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.LastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.AgentID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Email)
    </th>
    <th></th>
 </tr>

 @foreach (var item in Model) {
  <tr>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.AgentID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
        @Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
    </td>
  </tr>
}

 </table>

ClaimsManagementContext.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace ClaimsManagement2.Models
{
    public class ClaimsManagementContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }
}

Customer.cs

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

namespace ClaimsManagement2.Models
{
    [Table("Customers")]
    public class Customer
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int AgentID { get; set; }
        public string Email { get; set; }
    }
}

Aside from giving you a script of the database itself, i can't think of anything else to give you.

3
You need to include the full details of the error (i.e. the types involved) - List<T> is IEnumerable<T> so you must have different types involveduser3559349

3 Answers

0
votes

Just make your view expect a List instead of Enumerable or call AsEnumerable in your controller. You can do either this in your controller:

IEnumerable<Customer> customer = customerContext.Customers.AsEnumerable();

Or this in your view:

@model IList<ClaimsManagement2.Models.Customer>
0
votes

If you're only looping through the data in your view (as it appears that you are) and not adding or wishing to manipulatee the collection then simply convert your collection into an enumerable by calling .AsEnumerable() instead of ToList() in your controller, like so:

IEnumerable<Customer> customer = customerContext.Customers.AsEnumerable();

The IList interface implements additional methods for manipulating the collection and it's always recommended to implement only the functionality that you will required.

0
votes

I was following some bad instructions online and i created a Context class and a Customer class that I did not need. This was causing me problems. Unfortunately that is what happens when you follow youtube videos. All i needed were the classes created by the Entity Framework. I also did not need to put my context instantiation inside the action method.

Customer.cs and ClaimsManagementContext.cs needed to be deleted. These were created by me and not EF. I'm now sure when or why I would do this. But now i know i don't have to.