title: AJAX - Post to MVC Controller method without reloading page
body: <p>I created an MVC View page where, when a user clicks a button, a Controller method is called.  The method returns a text expression.  This expression is then displayed in a pop-up message.  </p>
  
  <p>The pop-up functionality works when the button is clicked, however, it has an unwanted side effect of causing the page to reload. Is there a way to prevent this?</p>
  
  <p>Here is the HTML which renders the button:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;button id="btnListGroups" title="List the groups the selected service ID is a member of"&gt;List Groups&lt;/button&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is the JavaScript:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function () {
      $("#btnListGroups").click(function () {
          var val = $("#ddlServiceID").val();
          $.post("/Home/GetGroups", { serviceID: val }, function (data, textStatus) 
          {
              alert(data);
          });
      });
  })</code></pre>
  </div>
  </div>
  </p>
  
  <p>And finally, here is the Controller method:</p>
  
  <pre><code>public partial class HomeController : Controller
  {
      public MvcHtmlString GetGroups(string serviceID)
      {
          ADHandler adHandler = new ADHandler();
  
          string groups = adHandler.GetGroupMemberships(serviceID);
          return new MvcHtmlString(groups);
      } 
  }
  </code></pre>