title: Submit data from textbox to asp net mvc controller using ajax
body: <p>I have this</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-inline", role = "form" }))
      {
          @Html.TextBox("SearchString", ViewBag.SearchFilter as string, new { @class = "form-control", onclick="keystroke()", id = "search" })
      }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>//GET: Home
  public async Task&lt;ActionResult&gt; Index(MyViewModel model)
              {
                  //...
                  if (model.SearchString != null)
                  {
                      //...
                      var myString = model.SearchString;
                      var b = model.FirstInt
                  }
                  //...
                  return View(model);
              }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>public class MyViewModel
  {
  //...
      public string SearchString { get; set; }
      public int? FirstInt { get; set; }
  }
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>function keystroke() {
          var firstint = 1;
          $("#search").keypress(function(event) {
              if (event.which === 13) {
                  event.preventDefault();
                  $("form").submit(function() {
                      var text = $('#SearchString').val();
                      sendForm(firstint, text);
                  });
              }
          });
      }
  function sendForm(firstint, text) {
              var data = {FirstInt: firstint, SearchString: text}
              $.ajax({
                  url: "Home/Index",
                  type: 'POST',
                  data: data,
                  success: function () {
                      alert('success');
                  }
              });
          };
  </code></pre>
  
  <p>and its does not work.</p>
  
  <p>I want to when user press the Enter key in search-input, I sent to controller the text which he entered, and the value of firstint. If I remove all javasript, then pressing Enter all normally sends to the controller but I need the firstint value too.</p>