title: Send data from textbox with ajax to asp.net mvc 5 controller
body: <p>I need some help. I write little app using ASP.NET MVC5 with JavaScript, jQuery, Ajax... and I can't send data from javascript to MVC Controller and change the model. </p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>public class MyViewModel
  {
  //...
      public string SearchString { get; set; }
      public int? FirstInt { get; set; }
      public int? SecondInt { get; set; }
  }
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>    function keystroke() {
          var a = 0, b = 0;
          $('#search').keyup(function (event) { a = 1; });
  
          $('#search').keydown(function (event) { b = 1; });
  
          $("#search").keypress(function (event) {
              if (e.which === 13) {
                  e.preventDefault();
                  $('form').click(function () {
                      sendForm(a, b);
                  });
              }
          });
      };
      function sendForm(a, b) {
          $.ajax({
              url: @Url.Action("Index", "Home"),
              type: 'POST',
              contentType: 'application/json',
              data: JSON.stringify({
                  FirstInt: a,
                  SecondInt: b
              }),
              success: function () {
                  alert('success');
              }
          });
      };
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-inline", role = "form" }))
              {
                  &lt;div class="form-group has-feedback"&gt;
                      @Html.TextBox("SearchString", ViewBag.SearchFilter as string, new
              {
                  @class = "form-control",
                  onclick="keystroke()",
                  id = "search"
              })
                  &lt;/div&gt;
              }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public async Task&lt;ActionResult&gt; Index(MyViewModel model)
          {
              //...
              if (model.SearchString != null)
              {
                  //...
                  var a = model.FirstInt;
                  var b = model.SecondInt;
              }
              //...
              return View(model);
          }
  </code></pre>
  
  <p>Help me, please, to send all the values to controller. Those that changed in JavaScript and what I enter in the textbox. Thanks.</p>