【问题标题】:Error in Visual Studio: Cannot implicitly convert type 'string' to 'int'Visual Studio 中的错误:无法将类型“string”隐式转换为“int”
【发布时间】:2023-03-05 03:46:01
【问题描述】:

这是作业,一个使用 C# 在 Visual Studio 中的 ASP.NET MVC 应用程序。当我运行它时,错误提示“无法将类型'string'隐式转换为'int'”,指的是以下行:Manufacturer = collection["Manufacturer"], Gears = collection["Gears"], Frame = collection[ "Frame"] 并且在 Gears = collection["Gears"] 下有一条波浪线。

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

【问题讨论】:

  • 认为这个错误意味着什么?
  • 我认为这意味着它拒绝了 Gears 的数据类型,它是一个 int。

标签: c# asp.net-mvc visual-studio-2012 controller


【解决方案1】:

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

试试下面的代码

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

【讨论】:

  • 那行得通。谢谢。我一开始并没有说 Gears 是一个 int。它是否按照列表中的第一个元素?
  • 不,与排序无关,与数据类型有关。
  • 是的,但为什么它认为 Gears 是一个字符串?
  • 因为collection 似乎是Dictionary&lt;String,string&gt; 类型的字典。
猜你喜欢
  • 1970-01-01
  • 2013-06-03
  • 2022-08-17
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多