title: Passing data from view to controller asp.net core razor pages
body: <p>I am trying to create a simple asp.net core razor web site.</p>
  
  <p>I have a cshtml page:</p>
  
  <pre><code>@page
  
  @using RazorPages
  
  @model IndexModel
  
  @using (Html.BeginForm()) {
    &lt;label for="age"&gt;How old are you?&lt;/label&gt;
    &lt;input type="text" asp-for="age"&gt;
    &lt;br/&gt;
    &lt;label for="money"&gt;How much money do you have in your pocket?&lt;/label&gt;
    &lt;input type="text" asp-for="money"&gt;
    &lt;br/&gt;
    &lt;input type="submit" id="Submit"&gt;
  }
  </code></pre>
  
  <p>and a cs file:</p>
  
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using System;
  using System.Threading.Tasks;
  
  namespace RazorPages
  {
    public class IndexModel : PageModel
    {
      protected string money { get; set; }
      protected string age { get; set; }
      public IActionResult OnPost()
      {
        if (!ModelState.IsValid)
        {
          return Page();
        }
  
  
  
        return RedirectToPage("Index");
  
      }
    }
  }
  </code></pre>
  
  <p>I want to be able to pass age and money to the cs file and then pass it back to the cshtml file in order to display it on the page after the submit button sends a get request. How can I implement this?</p>
  
  <p>Updated:
  The following code does not work.
  index.cshtml.cs:</p>
  
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using System;
  using System.Threading.Tasks;
  
  
  
  namespace RazorPages
  {
    public class IndexModel : PageModel
    {
      [BindProperty]
      public decimal Money { get; set; }
      [BindProperty]
      public int Age { get; set; }
      public IActionResult OnPost()
      {
   /*     if (!ModelState.IsValid)
        {
          return Page();
        }*/
      this.Money = Money;
      this.Age = Age;
  
  
  
  
  
  
  
   System.IO.File.WriteAllText(@"C:\Users\Administrator\Desktop\murach\exercises\WriteText.txt", 
  this.Money.ToString());
  return RedirectToPage("Index", new { age = this.Age, money = this.Money});
  
      }
    }
  }
  </code></pre>
  
  <p>and index.cshtml:</p>
  
  <pre><code> @page
      @using RazorPages
  
  
      @model IndexModel
  
      @using (Html.BeginForm()) {
        &lt;label for="Age"&gt;How old are you?&lt;/label&gt;
        &lt;input type="text" asp-for="Age"&gt;
        &lt;br/&gt;
        &lt;label for="Money"&gt;How much money do you have in your pocket?&lt;/label&gt;
        &lt;input type="text" asp-for="Money"&gt;
        &lt;br/&gt;
        &lt;input type="submit" id="Submit"&gt;
  
  
      }
      Money: @Model.Money
      Age: @Model.Age
  </code></pre>
  
  <p>Money and age show up as 0s on the page and file regardless of what you type in.</p>