Getting error: "does not contain a public definition for 'GetEnumerator'"

c# entity-framework

Question

I am trying to following this example but getting stuck to fetch data

I am getting error:

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'RMA.Models.RMAs' because 'RMA.Models.RMAs' does not contain a public definition for 'GetEnumerator'

Code Files Given below:

RMAModel.cs

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

namespace RMA.Models
{
    public class RMAs
    {
        [Key]
        public int RNumber { get; set; }
        public int OrderNumber { get; set; }
    }
}

DBContext Model.cs

using System.Data.Entity; 
using RMA.Models;

namespace RMA.DAL
{
    public class DBContext_Model:DbContext
    {
        public DbSet<RMAs> RMAs { get; set; }
    }
}

ListController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMA.DAL;
using RMA.Models;

namespace RMA.Controllers
{
    public class ListController : Controller
    {
        private DBContext_Model db = new DBContext_Model();

        public ActionResult Index()
        {
            var items = db.RMAs;
            return View(items.ToList());
        }

    }
}

index.cshtml

@model RMA.Models.RMAs

@{
    ViewBag.Title = "List of RMAs";
}

<h2>Index</h2>
@foreach (RMA.Models.RMAs i in Model)
{
    <span>@i.OrderNumber</span>
}

webconfig

<connectionStrings>
    <add name="DBContext_Model"
         connectionString="Server=xx\xx;Database=xxx;Integrated Security=True;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

I am using mvc3 Framework with .NET Framework 4 on VS2010

1
0
6/10/2014 10:14:53 PM

Accepted Answer

Your @model statement should be a list of RMAs (the name RMAs should probably be RMA; since each object represents a single RMA:

@model IEnumerable<RMA.Models.RMAs>   
4
6/10/2014 10:16:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow