title: ASP.NET Core Post form data IFormFile with ViewModel using HTTPClient
body: <p>I'm building WebAPI &amp; WebApp, both of them using ASP.NET Core 2.1</p>
  <p>My Web App is trying to send post request to the Web API using ViewModel that contains IFormFile and other properties. I know I have to use <code>MultipartFormDataContent</code> to post IFormFile, but I don't know how to implement it with my ViewModel because my ViewModel has <code>List</code> of other model.</p>
  <p>I already try to google some solutions, but I only found solutions with simple ViewModel without List like these :</p>
  <p><a href="https://stackoverflow.com/a/41511354/7906006">https://stackoverflow.com/a/41511354/7906006</a></p>
  <p><a href="https://stackoverflow.com/a/55424886/7906006">https://stackoverflow.com/a/55424886/7906006</a>.</p>
  <hr />
  <p>Is there any solution like</p>
  <pre><code>var multiContent = new MultipartFormDataContent();
  var viewModelHttpContent= new StreamContent(viewModel);
  MultiContent.Add(viewModelHttpContent, &quot;viewModel&quot;);
  var response = await client.PostAsJsonAsync(&quot;/some/url&quot;, multiContent);
  </code></pre>
  <p>so i don't have to add my property to <code>MultipartFormDataContent</code> one by one and post it as json.</p>
  <hr />
  <p>Here's my Web App ViewModel</p>
  <pre><code>public class CreateDataViewModel
  {
      public string PrimaryKeyNumber{ get; set; }
  
      public List&lt;Currency&gt; ListOfCurrency { get; set; }
  
      public IList&lt;DataDetail&gt; dataDetails { get; set; }
  
      [DataType(DataType.Upload)]
      public IFormFile Attachment { get; set; }
  
      //And other properties like Boolean, Datetime?, string
  }
  </code></pre>
  <p>Here's my Web App controller</p>
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; Create(CreateDataViewModel viewModel)
      {
          //How to implement MultipartFormDataContent with my ViewModel in here ?
  
          //My code below returns Could not create an instance of type Microsoft.AspNetCore.Http.IHeaderDictionary. Type is an interface or abstract class and cannot be instantiated. Path 'Attachment.Headers.Content-Disposition', line 1, position 723.
          //It works fine if I don't upload a file
          HttpResponseMessage res = await _client.PostAsJsonAsync&lt;CreateDataViewModel&gt;(&quot;api/data/create&quot;, viewModel);
  
          var result = res.Content.ReadAsStringAsync().Result;
  
          if (res.IsSuccessStatusCode)
          {
              TempData[&quot;FlashMessageSuccess&quot;] = &quot;Data have been submitted&quot;;
              return RedirectToAction(&quot;Index&quot;, &quot;Home&quot;); ;
          }
  
  
          //Code for error checking
  
      }
  </code></pre>
  <p>Here's my Web API controller that catches the post response using <code>CreateDataViewModel </code> as parameter.</p>
  <pre><code>[HttpPost]
  [Route(&quot;[action]&quot;)]
  public async Task&lt;IActionResult&gt; Create(CreateDataViewModel viewModel)
  {
      //Code to validate then save the data
  }
  </code></pre>