title: ASP.NET MVC Core TextBoxFor event pass model from View to Controller
body: <p>To be up front, my career has mostly been back end C# work and nothing on the front end, so my knowledge on all things front end is very limited.</p>
  
  <p>To begin, I have scoured everywhere looking for how to pass a model from the view to the controller when the enter key is pressed while in a textbox.  I have looked at ajax and js, but no example showed me how to pass in a model.  I got close and what resulted is the model was null while in the controller action method. As an example, the below code I would like to have an event on the Zip textbox when someone presses the Enter key while focus is in said textbox, it will pass the Model containing any and all values to the GotZip controller action.</p>
  
  <p>Any assistance would be greatly appreciated!</p>
  
  <p>Help me Obi-Wan Kenobi, you're my only hope...</p>
  
  <p><strong>Index.cshtml</strong></p>
  
  <pre><code>@model MyProject.Models.MyModel
  @{
      ViewBag.Title = "MyApp";
  }
  
  &lt;div class="text-left"&gt;
      &lt;form asp-controller="Home" asp-action="Index"&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.DisplayNameFor(model =&gt; model.Name)
                      @Html.TextBoxForFor(model =&gt; model.Name)
                  &lt;/th&gt;               
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.DisplayNameFor(model =&gt; model.Address1)
                      @Html.TextBoxForFor(model =&gt; model.Address1)
                  &lt;/th&gt;               
              &lt;/tr&gt;           
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.DisplayNameFor(model =&gt; model.City)
                      @Html.TextBoxForFor(model =&gt; model.City)
                  &lt;/th&gt;               
              &lt;/tr&gt;   
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.DisplayNameFor(model =&gt; model.State)
                      @Html.TextBoxForFor(model =&gt; model.State)
                  &lt;/th&gt;               
              &lt;/tr&gt;   
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.DisplayNameFor(model =&gt; model.Zip)
                      @Html.TextBoxForFor(model =&gt; model.Zip)
                  &lt;/th&gt;               
              &lt;/tr&gt;               
          &lt;/table&gt;
          &lt;input name="Submit" type="submit" asp-action="SubmitButton" value="Submit" /&gt;      
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>MyModel.cs</strong></p>
  
  <pre><code>namespace MyProject.Models
  {
      public class MyModel
      {
          public string Name { get; set; }
          public string Address1 { get; set; }
          public string City { get; set; }
          public string State { get; set; }
          public string Zip { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>HomeController.cs</strong></p>
  
  <pre><code>namespace MyProject.Controllers
  {
      [Authorize]
      public class HomeController : Controller
      {
          public IActionResult SubmitButton([FromForm] MyModel model)
          {
              //do stuff with model
  
              ModelState.Clear();
  
              return View("Index", myModel);
          }
  
          public IActionResult GotZip([FromForm] MyModel model)
          {
              //do stuff with model
  
              ModelState.Clear();
  
              return View("Index", myModel);
          }
  
      }
  }
  </code></pre>