1

当我尝试在控制器中接收列表时,出现错误:

"Error  1   The non-generic type 'Uppgift_1.Models.IProduct' cannot be used with type arguments"

我该如何解决这个问题,我真的以为我正在发送一个通用列表.. ??

另外,我不确定这是否正确:

public IEnumerable<MyProduct> pList = new IEnumerable<MyProduct>();

有时编译器会抛出一个错误,说我不能使 pList = new IEnumerable,但有时它可以。所以我不确定.. ??

你可以在这里抢我的代码: https ://github.com/xoxotw/mvc4_no1

我也把它贴在这里:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Uppgift_1.Models
{
    public interface IProduct
    {
        IQueryable<MyProduct> GetProducts();
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Uppgift_1.Models
{
    public class MyProduct 
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public double PriceBuy { get; set; }
        public double PriceSell { get; set { PriceSell = PriceBuy * Moms; } }
        public double Moms { get; set { value = 1.25; } }

        public MyProduct() 
        {

        }

        public MyProduct(int productId, string productName, double priceBuy)
        {
            ProductId = productId;
            ProductName = productName;
            PriceBuy = priceBuy;
        }

        public void CalcMoms()
        {
            // TODO: add calculation method

        }


    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Uppgift_1.Models
{
    public class MYcontext : MyProduct
    {
        public IEnumerable<MyProduct> pList = new IEnumerable<MyProduct>();

        public void FillMeUp()
        {

            MyProduct p1 = new MyProduct(21, "RollerSkates", 82.5);
            MyProduct p2 = new MyProduct(22, "Fridge", 88);
            MyProduct p3 = new MyProduct(23, "TV", 182.5);
            MyProduct p4 = new MyProduct(24, "Boat", 325);
            MyProduct p5 = new MyProduct(25, "Car", 22.5);
            MyProduct p6 = new MyProduct(26, "Magasine", 84.3);
            MyProduct p7 = new MyProduct(27, "Garage", 182.7);
            MyProduct p8 = new MyProduct(28, "House", 182.8);
            MyProduct p9 = new MyProduct(29, "Beach", 814.9);
            MyProduct p10 = new MyProduct(30, "Ball", 69.3);

            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            pList.Add(p6);
            pList.Add(p7);
            pList.Add(p8);
            pList.Add(p9);
            pList.Add(p10);
        }       
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Uppgift_1.Models
{
    public class MyRepository : IProduct
    {

        public IQueryable<MyProduct> Products
        {
            get { return MYcontext.pList; }

        }

        public IQueryable<MyProduct> GetProducts()
        {
            return (from obj in Products select obj).FirstOrDefault();
        }

    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Uppgift_1.Models;

namespace Uppgift_1.Controllers
{
    public class ProductController : Controller
    {
        IProduct<MyProduct> service = new MyRepository();      

        public ActionResult Index()
        {
            var prods = service.GetProducts();

            return View(prods);
        }

    }
}
4

1 回答 1

2

试试这个

在控制器中:

  List<MyProduct> list = new List<MyProduct>();
  //add data to list
  return View(list);

在视图中:

 @model IEnumerable<Uppgift_1.Models.MyProduct>
于 2013-08-28T09:16:42.717 回答