xoxo_tw
xoxo_tw

Reputation: 269

Receiving List in mvc controller

When I try to receive the list in my Controller, I get error:

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

How could I fix this, I really thought I was sending a generic List.. ??

Also , Im not sure if this is correct or not:

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

sometimes the compiler throw an error, saying I cant make pList = new IEnumerable, but sometimes its ok. so Im not sure about it.. ??

you could snatch my code here: https://github.com/xoxotw/mvc4_no1

And I post it here too:

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

    }
}

Upvotes: 1

Views: 248

Answers (1)

Boss
Boss

Reputation: 475

Try this

In Controller:

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

In View:

 @model IEnumerable<Uppgift_1.Models.MyProduct>

Upvotes: 2

Related Questions