Visual Studio 中的错误:无法将类型“string”隐式转换为“int”

本文关键字:string int 转换 类型 Studio 错误 Visual | 更新日期: 2023-09-27 18:33:12

这是家庭作业,Visual Studio中使用C#的 ASP.NET MVC应用程序。 当我运行它时,错误说,"无法隐式地将类型'字符串'转换为'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; }
    }
}

Visual Studio 中的错误:无法将类型“string”隐式转换为“int”

您需要显式将字符串转换为 int。

尝试以下行

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