title: MVC : Pass values from textbox to controller action
body: <p>I am new to MVC.Basically I need to pass values entered in the textbox from my view to controller action method. As I enter the values in the text box and click the enter button I need to display the value on the screen. I am currently unable to do so. Please find my code below
  The model class</p>
  
  <pre><code> public class ProteinTrackingService
      {
          public int? Total { get; set; }
          public int Goal { get; set; }
  
          public void AddProtein(int? amount)
          {
              Total += amount;
          }
  
      }
  </code></pre>
  
  <p>The controller class</p>
  
  <pre><code> public class ProteinTrackerController : Controller
      {
  
          ProteinTrackingService proteinTrackingService = new ProteinTrackingService();
          // GET: ProteinTracker
          public ActionResult Index()
          {
              ViewBag.Total = proteinTrackingService.Total;
              ViewBag.Goal = proteinTrackingService.Goal;
              return View();
          }
  
          // GET: ProteinTracker/Details/5
  
          public ActionResult AddProtein(ProteinTrackingService model)
          {
              proteinTrackingService.AddProtein(model.Total);
              ViewBag.Total = proteinTrackingService.Total;
              ViewBag.Goal = proteinTrackingService.Goal;
  
              return View("Index");
          }
  
  
      }
  </code></pre>
  
  <p>The view</p>
  
  <pre><code>using (Html.BeginForm("ProteinTracker", "AddProtein",FormMethod.Post))
  {
      @Html.AntiForgeryToken()
  
  
  
          &lt;form&gt;
              &lt;div class="form-horizontal"&gt;
                  &lt;h4&gt;Protein Tracker&lt;/h4&gt;
                  &lt;hr /&gt;
  
                  Total : @ViewBag.Total
                  Goal  : @ViewBag.Goal
  
                  &lt;input id="Text1" type="text" value="TextInput" /&gt;  &lt;input type="Submit" value="Add"  /&gt;
  
  
  
              &lt;/div&gt;
          &lt;/form&gt;
  
  }
  </code></pre>
  
  <p>I am modifying the code above based on your suggestions. I basically need to display the following in the view</p>
  
  <p>Total : value
  Goal : value</p>
  
  <p>Textbox control (To enter the total) Button (pass the total to contoller) Please note that when the user clicks the Add button the total should show in above field Total : value.</p>
  
  <p>New View</p>
  
  <pre><code>@using (Html.BeginForm( "AddProtein","ProteinTracker", FormMethod.Post))
  {
      @Html.AntiForgeryToken()
  
              &lt;div class="form-horizontal"&gt;
                  &lt;h4&gt;Protein Tracker&lt;/h4&gt;
                  &lt;hr /&gt;
  
                   @Html.LabelFor(m =&gt; m.Total, "Total" )  &lt;hr /&gt;
                   @Html.LabelFor(m =&gt; m.Goal, "Goal")   &lt;hr /&gt;
                   @Html.TextBoxFor(m =&gt; m.Total)   &lt;hr /&gt;
  
                   &lt;input type="Submit" value="Add"  /&gt;
              &lt;/div&gt;
  
  }
  </code></pre>
  
  <p>New Controller</p>
  
  <p>public class ProteinTrackerController : Controller
  {</p>
  
  <pre><code>ProteinTrackingService proteinTrackingService = new ProteinTrackingService();
  // GET: ProteinTracker
  public ActionResult Index()
  {
      var model = new ProteinTrackingService()
      { Total = proteinTrackingService.Total, Goal = proteinTrackingService.Goal };
      return View(model);
  
  }
  
  // GET: ProteinTracker/Details/5
  
  public ActionResult AddProtein(ProteinTrackingService model)
  {
      proteinTrackingService.AddProtein(model.Total);
      model.Total = proteinTrackingService.Total;
      model.Goal = proteinTrackingService.Goal;
  
      return View("Index",model);
  }
  </code></pre>
  
  <p>}</p>