title: Return IEnumerable Model to MVC Controller in MVC4
body: <p>I'm trying to bind viewmodel to a dataset which comes from json result in MVC.
  This is what I have:</p>
  
  <p>The function where I'm creating the json result:</p>
  
  <pre><code>private string WsUrl = "https://myUrl";
  public List&lt;RestCategories&gt; GetCategoryResults(string api, string site)
  {         
      List&lt;RestCategories&gt; categories = new List&lt;RestCategories&gt;();
      RestCategories cat;
      var webRequest = (HttpWebRequest)WebRequest.Create(WsUrl);
      webRequest.Method = "GET";
      try
      {
          var webResponse = (HttpWebResponse)webRequest.GetResponse();
          if ((webResponse.StatusCode == HttpStatusCode.OK) &amp;&amp; (webResponse.ContentLength &gt; 0))
          {
              var reader = new StreamReader(webResponse.GetResponseStream());
              string s = reader.ReadToEnd();
              var arr = JsonConvert.DeserializeObject&lt;JArray&gt;(s);
              int i = 1;               
  
              foreach (JObject obj in arr)
              {
                  cat = new RestCategories();
                  cat.DisplayInPrimaryCategoryListing = (bool)obj["DisplayInPrimaryCategoryListing"];
                  cat.ID = (int)obj["ID"];
                  cat.ItemCount = (int)obj["ItemCount"];
                  cat.Name = (string)obj["Name"];
                  cat.Order = (int)obj["Order"];
                  cat.SubCategoryOf = (string)obj["SubCategoryOf"].ToString(); 
                  categories.Add(cat);
              }
              return categories;
          }
      }
      catch
      {
          throw;
      }
      return categories;
  }
  </code></pre>
  
  <p>This is what I have for <code>CategoriewViewModel</code>:</p>
  
  <pre><code>public class CategoriesViewModel
      {
          public IEnumerable&lt;RestCategories&gt; Categories;
      }
  </code></pre>
  
  <p><code>RestCategories</code> definition:</p>
  
  <pre><code>public class RestCategories
      {           
          public int ID { get; set; }
          public int ItemCount { get; set; }
          public string Name { get; set; }            
      }
  </code></pre>
  
  <p>And finally in the controller I want to make something like this:</p>
  
  <pre><code>[HttpGet]
  public ActionResult Categories()
  {            
      MyModel modelApi = new MyModel();
      List&lt;RestCategories&gt; itemsResult = modelApi.GetCategoryResults("test", "test");
      CategoriesViewModel modelCat = new CategoriesViewModel
      {
          //I need some magic here to return ienumerable dataset of RestCategories
          Categories = itemsResult.                                   
      };
      return View(modelCat);
  }
  </code></pre>
  
  <p>Now, as you can see I want to return <code>IEnumerable</code> type of model, since I'll have multiple results from the web service call. Maybe I need to do the <code>GetCategoryResults</code> method to return multiple results?  Even if I do that how can I then look through the returned object in the controller to get what I want? </p>