在mvc控制器中接收列表

时间:2013-08-28 09:04:29

标签: c# asp.net-mvc-4 generics interface

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

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

我怎么能解决这个问题,我真的以为我发送了一个通用的List .. ??

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

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

    }
}

1 个答案:

答案 0 :(得分:2)

试试这个

在控制器中:

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

在视图中:

 @model IEnumerable<Uppgift_1.Models.MyProduct>