2013-09-23 22 views
1

我正在使用Visual Studio 2012.以下是BikeController和Bike Model。當我運行該程序時,錯誤是「無法使用集合初始值設定項初始化類型'MvcApplication3.Models.Bike',因爲它沒有實現'System.Collections.IEnumerable'。」錯誤位於bikes = new Bike {行,但是當我將using System.Collections.IEnumerable;放置在Bike Model中時,它說:「使用命名空間指令只能應用於命名空間;'System.Collections.IEnumerable'是類型而不是命名空間。提前致謝。如何在C#中用ASP.NET MVC中的控制器解決集合錯誤?

自行車控制器:

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(); 
      } 
     } 
    } 
} 

自行車型號:

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

namespace MvcApplication3.Models 
{ 
    public class Bike 
    { 

     public int BikeID { get; set; } 
     public string Manufacturer { get; set; } 
     public int Gears { get; set; } 
     public string Frame { get; set; } 

     static protected int bikeCount; 

     public Bike() 
     { 
      this.Manufacturer = "Schwinn"; 
      this.Gears = 10; 
      this.Frame = "Mountain"; 
      this.BikeID = bikeCount; 
      bikeCount++; 
     } 
    } 

} 

回答

0

初始化控制器是這樣的:

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" } 
      }; 
     } 

     ... 
    } 
+0

非常感謝!我可以問你另一個問題嗎?公共ActionResult編輯(INT ID) { return View(bike [id]); } – Grafica

+0

try:return View(bikes.Where(b => b.BikeID == id).first()); – Pascalz

+0

它說'System.Collections.Generic.IEnumerable '沒有包含'first'的定義,也沒有包含接受類型的第一個參數的擴展方法'first'Error 'System.Collections。 Generic.IEnumerable '不包含'first'的定義,也沒有接受'System.Collections.Generic.IEnumerable '類型的第一個參數的擴展方法'first'被發現。 – Grafica

1

聲明你的變量作爲自行車的列表

List<Bike> bikes; 

,然後初始化它

bikes = new List<Bike>() { 
          new Bike(), 
          new Bike() { ... }, 
          ... 
         }; 
0

你必須使用目錄<>自行車。

相關問題