title: How to use buttons in Asp.Net to call a method
body: <p>I am trying to make a button in my View call a method in the Model using the button's onClick event, but I am not sure how to make this work. The button just doesn't do anything.</p>
  
  <p>This is my View:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;div&gt;
          &lt;p&gt;Coins: @Model.Coins&lt;/p&gt;
          &lt;button type="button" id="btnScore"
                  onclick="Btn_Click"&gt;Click me!&lt;/button&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is the model I try to call the method in:</p>
  
  <pre><code>public class ClickerGame
  
      {
          public int Coins { get; set; }
  
          public ClickerGame()
          {
              Coins = 0;
          }
  
          public void Btn_Click()
          {
              Coins++;
          }
  }
  </code></pre>
  
  <p>This is the Controller:</p>
  
  <pre><code>public class ClickerController : Controller
      {
          // GET: /&lt;controller&gt;/
          public IActionResult Index()
          {
              ClickerGame model = new ClickerGame();
              return View(model);
          }
      }
  </code></pre>
  
  <p>And this is my Startup.cs</p>
  
  <pre><code>public class Startup
  {
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddMvc();
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
      {
          loggerFactory.AddConsole();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Clicker}/{action=Index}/{id?}");
          });
      }
  }
  </code></pre>
  
  <p>I am able to get the amount of coins in calling @Model.Coins in the View, but when I try to use @Model.Btn_Click it gives me an error saying the method can't be converted to a delegate object.
  I am using Asp.Net Core 1.0.1 in visual studio.</p>
  
  <p><strong>Edit:</strong>
  I changed the Controller and the View and it now is able to call the method, but when it tries to increase the coin variable I get a NullReference exception... </p>
  
  <p>The View:</p>
  
  <pre><code>@model AspNetClicker.Models.ClickerGame
  
  &lt;script&gt;
      function IncrementCount()
      {
          location.href = "@Url.Action("IncrementCount")";
      }
  &lt;/script&gt;
  
  
  &lt;html&gt;
  
  &lt;body&gt;
      &lt;div&gt;
          &lt;p&gt;Coins: @Model.Coins&lt;/p&gt;
          &lt;button type="button" id="btnScore"
                  onclick="IncrementCount()"&gt;
              Click me!
          &lt;/button&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>ClickerGame:</p>
  
  <pre><code>public class ClickerGame
  
      {
          public int Coins { get; set; }
  
          public ClickerGame()
          {
              Coins = 0;
          }
  }
  </code></pre>
  
  <p>ClickerController:</p>
  
  <pre><code>public class ClickerController : Controller
  {
      ClickerGame model;
  
      public IActionResult Index()
      {
          model = new ClickerGame();
          return View(model);
      }
  
      public void IncrementCount()
      {
          model.Coins++;
      }
  }
  </code></pre>