locked
MVC 5 question - How to pass list from controller to view RRS feed

  • Question

  • I'm new to MVC.  I have a controller with this code:           

    public ActionResult QuoteDetails()
            {
                //
                // Shopping Cart
                //                
                if (cart.Count == 0)
                    return RedirectToRoute("ShoppingCart");

                Int32 i = 0; 
                List<string> myProd = new List<string>();
                List<string> mySku = new List<string>();
                List<Single> myQty = new List<Single>();
                List<string> myList = new List<string>();

                foreach (ShoppingCartItem sci in cart)
                {
                        myList.Add(sci.Product.Name + " " + sci.Product.Sku + " " + sci.Quantity);
                        myProd.Add(sci.Product.Name);
                        mySku.Add(sci.Product.Sku);
                        myQty.Add(sci.Quantity);

                        i ++;
                }
                    return View();
            }

    What I want to do is display product data in a table-like format in my view.  Like this:

    Product Name                             Sku                              Quantity

    aaaaaaaaaaaaaaaaaaaa               111111111                   5

    bbbbbbbbbbbbbbb                    22222                           10

    ccccccccccccccccccccccccccc       333333333333             3

    I tried several ways to pass this data but nothing seems to work.  As you can see, I created List myList which allows me to add all 3 fields I want to pass to my view.  myList does contain all the data I need but not sure how to send this to my view.  I can do it using ViewBag but I read that this is not the way to do it.  I did try ViewBag.myList, but I don't know how to make myList display in a table format like I presented above.  I also created Lists for each item I want to save but wasn't sure how to pass each list to my view and loop thru in my view to list all my data in a table.

    My approach may be all wrong as well.  Is there a better way to accomplish this?

    Can anyone help me?  I think if I can get a good working example of this then I will be on my way.




    • Edited by bh052699 Thursday, June 18, 2015 1:30 PM
    • Moved by Mike Danes Thursday, June 18, 2015 2:10 PM asp.net related
    Thursday, June 18, 2015 1:21 PM

Answers

All replies