title: Submit a Form using AJAX in ASP.Net Core MVC
body: <p>I am working with ASP.Net Core 2.1, and trying to upload a file while returning it's url, without refreshing the page. </p>
  
  <p>I am trying to write the JavaScript in site.js as the _RenderPartial("scripts") renders all scripts at the end of the page and hence directly using script tag in the razor view is not working. Secondly, adding it to site.js gives me an opportunity to call the script across the site views.</p>
  
  <p>My Controller action looks like :</p>
  
  <pre><code>    [HttpPost]
      [DisableRequestSizeLimit]
      public async Task&lt;IActionResult&gt; Upload()
      {
        // Read &amp; copy to stream the content of MultiPart-Form
        // Return the URL of the uploaded file
        return Content(FileName);
      }
  </code></pre>
  
  <p>My view looks like :</p>
  
  <pre><code>&lt;form id="FileUploadForm" action="~/Resources/Upload" method="post" enctype="multipart/form-data"&gt;
  &lt;input name="uploadfile" type="file" /&gt;
  &lt;button name="uploadbtn" type="submit" onclick="SubmitForm(this.parentElement, event)"&gt;Upload&lt;/button&gt;
  </code></pre>
  
  <p></p>
  
  <p>The site.js currently looks like :</p>
  
  <pre><code>function SubmitForm(form, caller) {
  caller.preventDefault();
  $.ajax(
      {
          type: form.method,
          url: form.action,
          data: form.serialize(),
          success: function (data) { alert(data); },
          error: function (data) { alert(data); }
      })}
  </code></pre>
  
  <p>Presently, the code bypasses the entire script and the file is uploaded and new view displaying the file name is returned. I need help to create the javascript.</p>