简体   繁体   English

如何在C#中使用ASP.NET MVC中的控制器解决集合错误?

[英]How do I resolve collection error with my controller in ASP.NET MVC in C#?

I'm using Visual Studio 2012. Here are the BikeController and Bike Model. 我正在使用Visual Studio2012。这是BikeController和Bike模型。 When I run the program the error is "Cannot initialize type 'MvcApplication3.Models.Bike' with a collection initializer because it does not implement 'System.Collections.IEnumerable'." 当我运行程序时,错误是“无法使用集合初始化程序初始化类型'MvcApplication3.Models.Bike',因为它未实现'System.Collections.IEnumerable'。” The error is on the line bikes = new Bike { but when I placed using System.Collections.IEnumerable; 错误是在线上的bikes = new Bike {但是当我using System.Collections.IEnumerable;放置时using System.Collections.IEnumerable; in the Bike Model, it said "A using namespace directive can only be applied to namespaces; 'System.Collections.IEnumerable' is a type not a namespace." 在Bike Model中,它说“使用名称空间指令只能应用于名称空间;'System.Collections.IEnumerable'是类型,而不是名称空间。” Thanks in advance. 提前致谢。

Bike Controller: 自行车控制器:

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

Bike Model: 自行车型号:

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

}

Declare your variable bikes as a list 将您的可变自行车声明为列表

List<Bike> bikes;

and then instantiate it 然后实例化它

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

Initialize controller like this : 像这样初始化控制器:

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

        ...
   }

您必须对自行车使用List <>。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# ASP.Net 我如何将我的 sqlite 数据库连接到 home controller - C# ASP.Net How do I give my sqlite database connection to the home controller 使用C#在ASP.NET MVC 4中脚手架控制器时出错 - Error when scaffolding controller in asp.net mvc 4 with c# C#ASP.NET MVC:如何让浏览器从我的响应中自动下载pdf? - C# ASP.NET MVC: How do I get the browser to automatically download a pdf from my response? (C#) 如何将图像文件从 ASP.NET MVC 5 web 表单发送到我的 SQL 数据库? - (C#) How do I send an image file from an ASP.NET MVC 5 web form to my SQL database? 如何将 ID 传递给 ASP.NET MVC 中的控制器? - How do I pass an ID to the controller in ASP.NET MVC? 如何在ASP.NET MVC中允许/ Controller / Id? - How do I allow /Controller/Id in ASP.NET MVC? 如何在ASP.NET MVC 5项目中获得“添加控制器”和“添加视图”菜单选项? - How do I get the “Add Controller” and “Add View” menu options in my ASP.NET MVC 5 project? 我如何告诉 MVC 中间件我的 class 是 ASP.NET Core 中的一个 Controller? - How do I tell the MVC middleware that my class is a Controller in ASP.NET Core? 如何限制ASP.NET MVC应用程序中控制器中的动作? - How do I limit the actions in my controller in an ASP.NET MVC application? 如何重新格式化ASP.NET MVC5错误消息以仅显示C#中的第一条消息 - How can I reformat my ASP.NET MVC5 error messages to show only the first message in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM