c# - Receiving List in mvc controller -
when try receive list in controller, error:
"error 1 non-generic type 'uppgift_1.models.iproduct' cannot used type arguments"
how fix this, thought sending generic list.. ??
also , im not sure if correct or not:
public ienumerable<myproduct> plist = new ienumerable<myproduct>();
sometimes compiler throw error, saying cant make plist = new ienumerable, ok. im not sure it.. ??
you snatch code here: https://github.com/xoxotw/mvc4_no1
and post 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 { { 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); } } }
try this
in controller:
list<myproduct> list = new list<myproduct>(); //add data list return view(list);
in view:
@model ienumerable<uppgift_1.models.myproduct>
Comments
Post a Comment