Pass a generic list from controller to view mvc
<p>I've racked my brain over this for way to long now to I'm deferring to the experts. I know this question has been asked and answered several times but I can't seem to get ANYTHING to work. Here's the scenario: As the title says, I'm trying to pass a list from the controller to the view. I'm using an API that has a method, <code>"GetInventoryLocations"</code> whose base type is <code>List&lt;string&gt;</code>. In the example below I instantiate a new list and use a <code>foreach</code> to loop through the <code>"InventoryLocation"</code> programatically casting each item in the collection to a string and adding it to the list I created <code>"locationlist"</code>. Finally I assign the list to the <code>viewdata</code>. From there I've tried all kinds of things in the view but still can't get it to work. Thanks for any help. Be kind to a junior developer. </p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Moraware.JobTrackerAPI4;
using Evolveware1_0.Models;

namespace Evolveware1_0.Controllers
{
    [Authorize]
    public class InventoryController : Controller
    {

        //./Inventory/Locations
        [HttpGet]
        public ActionResult Index()
        {
            //declare variables for connection string to JobTracker API Service
            var DB = "databasename"; // your DB name here
            var JTURL = "https://" + DB + ".somecompany.net/" + DB + "/";
            var UID = "****"; // your UID here - needs to be an administrator or have the API role
            var PWD = "password"; // your PWD here

            //connect to API
            Connection conn = new Connection(JTURL + "api.aspx", UID, PWD);
            conn.Connect();
            //declaring the jobtracker list (type List&lt;InventoryLocation&gt;)
            var locs = conn.GetInventoryLocations();
            //create a new instance of the strongly typed List&lt;string&gt; from InventoryViewModels
            List&lt;string&gt; locationlist = new List&lt;string&gt;();
            foreach (InventoryLocation l in locs) {
                locationlist.Add(l.ToString());                  
            };
            ViewData["LocationsList"] = locationlist;

            return View();
        }//end ActionResult
    }

};
</code></pre>

<p>And in the view:</p>

<pre><code>@using Evolveware1_0.Models
@using Evolveware1_0.Controllers
@*@model Evolveware1_0.Models.GetLocations*@

@using Evolveware1_0.Models;
@{
    ViewBag.Title = "Index";
}


&lt;h2&gt;Locations&lt;/h2&gt;

@foreach (string l in ViewData["LocationList"].ToString())
{
    @l
}
</code></pre>