我正在尝试关注this示例,但却陷入了获取数据
的问题我收到错误:
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'
下面给出的代码文件:
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>
我在VS2010上使用mvc3 Framework和.NET Framework 4
答案 0 :(得分:4)
你的@model语句应该是一个RMA列表(名称RMA应该是RMA;因为每个对象代表一个RMA:
@model IEnumerable<RMA.Models.RMAs>