title: Asp.net Core, How to post file and model to controller in JQuery/Ajax?
body: <p><strong>This question is not a repetition with <a href="https://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc">this</a> because all details are fully described in this question and will be very suitable for beginners</strong></p>
  
  <p>I have a view that inherits from a model. my field are in a form and I want to post form (or Model) to controller with JQuery/Ajax and insert it in database. I read <a href="http://www.binaryintellect.net/articles/f1cee257-378a-42c1-9f2f-075a3aed1d98.aspx" rel="noreferrer">this</a> article and can upload image with ajax but i can not post whole form or model with data to controller.</p>
  
  <p><strong>my view :</strong></p>
  
  <pre><code>@model ajax1.Models.Info
  
  &lt;form method="post" enctype="multipart/form-data"&gt;
  
  &lt;div class="form-group"&gt;
      &lt;label asp-for="FirstName" class="col-lg-2 col-sm-2 control-label "&gt;&lt;/label&gt;
      &lt;div class="col-lg-6"&gt;
          &lt;input asp-for="FirstName" class="form-control" /&gt;
          &lt;span asp-validation-for="FirstName"&gt;&lt;/span&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
      &lt;label asp-for="LastName" class="col-lg-2 col-sm-2 control-label"&gt;&lt;/label&gt;
      &lt;div class="col-lg-6"&gt;
          &lt;input asp-for="LastName" class="form-control" /&gt;
          &lt;span asp-validation-for="LastName"&gt;&lt;/span&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;input type="file" id="files" name="files" multiple /&gt;
  &lt;input type="button" id="upload"  value="Upload Selected Files" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>JQuery Code at the bottom of view:</strong></p>
  
  <pre><code>@section Scripts{
  &lt;script&gt;
          $("#upload").click(function (evt) {
              var fileupload = $("#files").get(0);
              var files = fileupload.files;
              var data = new formdata();
              for (var i = 0; i &lt; files.length; i++) {
                  data.append(files[i].name, files[i]);
              }
              $.ajax({
                  type: "post",
                  url: "/home/uploadfilesajax",
                  contenttype: false,
                  processdata: false,
                  data: data,
                  success: function (message) {
                      alert(message);
                  },
                  error: function () {
                      alert("there was error uploading files!");
                  }
              });
          });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Controller :</strong></p>
  
  <pre><code>    [HttpPost]
      public async Task&lt;IActionResult&gt; UploadFilesAjax(Info model)
      {
          var files = HttpContext.Request.Form.Files;
          foreach (var Image in files)
          {
              if (Image != null &amp;&amp; Image.Length &gt; 0)
              {
                  var file = Image;
                  var uploads = Path.Combine(_appEnvironment.WebRootPath, "img\\");
                  if (file.Length &gt; 0)
                  {
                      var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName);
                      using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create))
                      {
                          await file.CopyToAsync(fileStream);
                          model.imgName = fileName;
                      }
                  }
              }
          }
  
          using (var db = _iServiceProvider.GetRequiredService&lt;ApplicationDbContext&gt;())
          {
  
              db.Info.Add(model);
              db.SaveChanges();
          }
          string message = "success";
          return Json(message);
      }
  </code></pre>
  
  <p><strong>Model :</strong></p>
  
  <pre><code>public class Info
  {
      [DisplayName("First Name")]
      [Required(ErrorMessage = "Enter First Name")]
      public string FirstName { get; set; }
  
      [DisplayName("Last Name")]
      [Required(ErrorMessage = "Enter Last Name")]
      public string LastName { get; set; }
  
      [DisplayName("select image")]
      public string imgName { get; set; }
  }
  </code></pre>
  
  <p>When i put a breakpoint on the action and click on the submit button the application dose not go to action. How can i post all form or model to controller? I want if 'ModelStates.IsValid == false' the validation error shows.</p>