简体   繁体   中英

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. 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'." The error is on the line bikes = new Bike { but when I placed 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." 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 <>。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM