title: Use Javascript (jQuery) to display result of request in ASP.NET MVC
body: <p>I have an ASP.NET MVC project and currently I am using <code>ViewData["ResultMessage"]</code> to show the result of a form (which is done after the user submits &amp; refreshes the page).</p>
  
  <p>I want to do it right after he clicks submit (without refreshing the page). So I have to use Javascript for that, which I am new with. No experience what so ever.</p>
  
  <p>How my form looks like is this:</p>
  
  <pre><code>            &lt;form id="contactForm" method="post" action=".."&gt;
                  &lt;div class="field half first"&gt;
                      &lt;label for="Name"&gt;Name&lt;/label&gt;
                      &lt;input type="text" name="Name" id="Name" placeholder="Your name" required /&gt;
                  &lt;/div&gt;
                  &lt;div class="field half"&gt;
                      &lt;label for="Email"&gt;Email&lt;/label&gt;
                      &lt;input type="email" name="Email" id="Email" placeholder="Your email" required /&gt;
                  &lt;/div&gt;
                  &lt;div class="field"&gt;
                      &lt;label for="Message"&gt;Message&lt;/label&gt;
                      &lt;textarea name="Message" id="Message" rows="5" placeholder="Your message" required minlength="20"&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
  
                  &lt;input type="submit" value="Send Message" /&gt;
                  &lt;p&gt;@ViewData["ContactActionResult"]&lt;/p&gt;
  
              &lt;/form&gt;
  </code></pre>
  
  <p>You can see the result is displayed with Razor using <code>ViewData</code>. Now I want to do it without refreshing the page and learn JS on the way.</p>
  
  <p>What I have tried so far:</p>
  
  <pre><code>$(function () {
      $("button").click(function (e) {
          e.preventDefault()
          $.ajax({
              type: "POST",
              url: "/Receiver",
              data: car,
              datatype: "html",
              success: function (data) {
                  alert("Congratulations, it worked!");
                  $('#contactForm').html(data);
              },
              error: function(data) {
                  alert("Failed.");
              }
          });
      });
  });
  </code></pre>
  
  <p>But I am not sure if I did something wrong or simply not implementing it right. I have the above function inside <code>&lt;script&gt; here &lt;/script&gt;</code> in my HTML page. Nothing happens when I click the submit button on the form.</p>