如何在C#中的ASP.NET MVC中解决控制器的收集错误?
我正在使用Visual Studio 2012。这是自行车控制器和自行车模型。当我运行该程序时,错误是“无法使用集合初始值设定项初始化类型‘mvcapapplication3.Models.Bike’,因为它没有实现‘System.Collections.IEnumerable’。”错误在如何在C#中的ASP.NET MVC中解决控制器的收集错误?,c#,asp.net-mvc,visual-studio-2012,C#,Asp.net Mvc,Visual Studio 2012,我正在使用Visual Studio 2012。这是自行车控制器和自行车模型。当我运行该程序时,错误是“无法使用集合初始值设定项初始化类型‘mvcapapplication3.Models.Bike’,因为它没有实现‘System.Collections.IEnumerable’。”错误在bikes=new Bike{行,但当我在自行车模型中放置using System.Collections.IEnumerable;时,它说“using namespace指令只能应用于名称空间;'System
bikes=new Bike{
行,但当我在自行车模型中放置using System.Collections.IEnumerable;
时,它说“using namespace指令只能应用于名称空间;'System.Collections.IEnumerable'是一种类型,而不是名称空间。”请提前感谢
自行车控制器:
using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class BikeController : Controller
{
//
// GET: /Bike/
Bike bikes;
public BikeController()
{
bikes = new Bike {
new Bike(),
new Bike { Manufacturer = "Nishiki", Gears = 5, Frame = "Road" }
};
}
public ActionResult Index()
{
return View(this.bikes);
}
private ActionResult View(Func<object> func)
{
throw new NotImplementedException();
}
//
// GET: /Bike/Details/5
public ActionResult Details(Bike b)
{
return View(b);
}
//
// GET: /Bike/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Bike/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Bike/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Bike/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Bike/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Bike/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
将可变自行车声明为列表
List<Bike> bikes;
列出自行车;
然后实例化它
bikes = new List<Bike>() {
new Bike(),
new Bike() { ... },
...
};
bikes=新列表(){
新自行车(),
新自行车(){…},
...
};
将您的可变自行车声明为列表
List<Bike> bikes;
列出自行车;
然后实例化它
bikes = new List<Bike>() {
new Bike(),
new Bike() { ... },
...
};
bikes=新列表(){
新自行车(),
新自行车(){…},
...
};
如下初始化控制器:
public class BikeController : Controller
{
//
// GET: /Bike/
List<Bike> bikes;
public BikeController()
{
bikes = new List<Bike>() {
new Bike(),
new Bike { Manufacturer = "Nishiki", Gears = 5, Frame = "Road" }
};
}
...
}
公共类自行车控制器:控制器
{
//
//骑自行车/
列出自行车;
公共自行车控制器()
{
自行车=新列表(){
新自行车(),
新自行车{制造商=“Nishiki”,档位=5,车架=“道路”}
};
}
...
}
如下初始化控制器:
public class BikeController : Controller
{
//
// GET: /Bike/
List<Bike> bikes;
public BikeController()
{
bikes = new List<Bike>() {
new Bike(),
new Bike { Manufacturer = "Nishiki", Gears = 5, Frame = "Road" }
};
}
...
}
公共类自行车控制器:控制器
{
//
//骑自行车/
列出自行车;
公共自行车控制器()
{
自行车=新列表(){
新自行车(),
新自行车{制造商=“Nishiki”,档位=5,车架=“道路”}
};
}
...
}
你必须使用自行车列表。你必须使用自行车列表。非常感谢!我可以问你另一个问题吗?我的返回语句在这里应该怎么读:public ActionResult Edit(int id){return View(bike[id]);}try:return View(bikes.Where(b=>b.BikeID==id).first());它表示“System.Collections.Generic.IEnumerable”不包含“first”的定义,并且没有扩展方法“first”接受类型为错误1的第一个参数“System.Collections.Generic.IEnumerable”不包含“first”的定义,也没有扩展方法“first”接受类型为“System.Collections.Generic.IEnu”的第一个参数可以找到merable。Ho,它的第一个()很抱歉,它说:类型或名称空间名称“Bikes”在名称空间“mvcapapplication3.Models”中不存在。非常感谢!我可以问你另一个问题吗?我的return语句在这里应该怎么读:public ActionResult Edit(int id){return View(bike[id]);}try:return View(bikes.Where(b=>b.BikeID==id.first());它表示“System.Collections.Generic.IEnumerable”不包含“first”的定义,并且没有扩展方法“first”接受类型为错误1的第一个参数“System.Collections.Generic.IEnumerable”不包含“first”的定义,也没有扩展方法“first”接受类型为“System.Collections.Generic.IEnu”的第一个参数可以找到merable。Ho,它的第一个()sorry它说:类型或名称空间名称“Bikes”在名称空间“mvcapapplication3.Models”中不存在。