title: Return Partial View in Ajax Success Call with Fetched Json Data
body: <p>I am fetching JSON data via an AJAX call to a controller within an ASP.NET MVC project.  I am able to get the JSON data from the Web API call just fine.  Now I want to take this JSON data that is returned to the <code>success</code> function within my AJAX call and pass it to a partial view instead of statically appending JSON data with jQuery.</p>
  
  <p><strong>C# Controller:</strong> </p>
  
  <pre><code>[HttpGet]
  [Route("company-info/companyinfogetapidata")]
  [AllowAnonymous]
  public ActionResult CompanyInfoGetApiData(string name, int CompanyCode, string city, string state, int zip)
      {
  
          HttpClient client = new HttpClient();
  
          client.BaseAddress = new Uri("URL_BASE");
          client.DefaultRequestHeaders.Accept.Clear();
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
          var request = client.GetAsync("URL_PATH");                
          var json = request.Result.Content.ReadAsStringAsync().Result;
  
          JObject o = JObject.Parse(json);
  
          JToken ApiData = o["results"];
  
         // Here is where I want to insert pieces of the JSON data into the model and then pass it into the partial view
          CompanyResultsModel getfetcheddata = new CompanyResultsModel();
  
          getfetcheddata = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;CompanyResultsModel&gt;(json);
  
  
          return PartialView(@"~/Views/Shared/companies/_companyResults.cshtml", getfetcheddata);
  
      }
  </code></pre>
  
  <p><strong>EDIT:</strong> I created a new model below
  <code>CompanyResultsModel:</code></p>
  
  <pre><code>namespace Companies.Models
  {
      public class CompanyResultsModel
      {
          public string companyName { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>AJAX Call:</strong></p>
  
  <pre><code>$.ajax({
      type: "GET",
      url:"../companyinfogetapidata",
      dataType: "json",
      data: {DATA HERE},
      error:function(e){alert("nope"+e);},
      success: function (xhr_request) {
  
          var fetched_data = xhr_request["results"];
  
          var i;
  
          var iLength = fetched_data.length;
  
          for (i = 0; i &lt; iLength; i++) {
              // This was the original jQuery way I was appending the JSON data
              // $("#CompanyFinderResultsContainer").append("&lt;p&gt;Name:&amp;nbsp;" + fetched_data[i].name + "&lt;/p&gt;");
              // I want to call a Partial View and Pass along the JSON data from this success function
              // I am thinking of making a call like this                            
              @Html.Partial("_CompanyFinderResults", Model);
  
          }
  
  
      }
  });
  </code></pre>
  
  <p>I tried following a few methods of creating a model within the controller to append this JSON data but I'm not quite sure if this is the best approach.</p>
  
  <p>Any help is greatly appreciated!</p>