2013-09-30 169 views
0

這是家庭作業,它是Visual Studio中使用C#的ASP.NET MVC應用程序。當我運行它時,錯誤說:「不能隱式地將類型'字符串'轉換爲'int',」參考下面這行:Manufacturer = collection [「Manufacturer」],Gears = collection [「Gears」],Frame = collection [ 「Frame」],Gears = collection [「Gears」]下面有一條波浪線。Visual Studio中的錯誤:無法將類型'字符串'隱式轉換爲'int'

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/ 

     List<Bike> bikes; 


     public BikeController() 
     { 
      if (bikes == null) 
      { 
       bikes = new List<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(int id) 
     { 
      var currentBikes = bikes[id]; 
      return View(currentBikes); 

     } 

     // 
     // GET: /Bike/Create 

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

     // 
     // POST: /Bike/Create 

     [HttpPost] 
     public ActionResult Create(FormCollection collection) 
     { 
      Bike b = new Bike 
      { 
       Manufacturer = collection["Manufacturer"], Gears = collection["Gears"], Frame = collection["Frame"] 
      }; 
      bikes.Add(b); 
      try 
      { 
       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // 
     // GET: /Bike/Edit/5 

     public ActionResult Edit(int id) 
     { 
      return View(bikes.Where(b => b.BikeID == id).First()); 
     } 

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

     public int bike { get; set; } 
    } 
} 
+1

你怎麼_you_認爲錯誤意味着什麼? –

+0

我認爲這意味着它拒絕了Gears的數據類型,這是一個int。 – Grafica

回答

0

您需要將字符串顯式轉換爲int。

嘗試以下行

Manufacturer = collection["Manufacturer"], Gears = int.Parse(collection["Gears"]), Frame = collection["Frame"] 
+0

工作。謝謝。我沒有說Gears首先是一個整數。它是按照列表中的第一個元素進行的嗎? – Grafica

+0

不,它與排序無關,但與數據類型有關。 – Tilak

+0

是的,但它爲什麼認爲齒輪是一個字符串? – Grafica

相關問題