Asp.Net-Core with Razor Pages Bind hidden input

Viewed 1532

In my java script file I am setting the value of a hidden input like this:

document.getElementById("myVal").value = 11;

In my html:

<input asp-for="Value" id="myVal" />

In my Asp.Net-Core Web Application with Razor Pages I am trying to bind this property so I can take the value :

public class IndexModel : PageModel
{
   [HiddenInput] //I also tried to add [BindProperty] after
   public int Value { get; set; }

   public void OnGet()
   {
       Debug.WriteLine(Value);
   }
}

EDIT

function redirect() {
    document.getElementById("myVal").value = 11;
}

This function is called when I click a button, and that is when the value is being set to 11.

What I want is that when this button is clicked, I can save the value myVal so that I can use it in my Razor Page.

The problem is that the value of the field will always be 0 (even when the field is not hidden). How can I set the value for the hidden field?

1 Answers

This work for me

  • i define post method

      [BindProperty]
      [HiddenInput] 
      //I also tried to add [BindProperty] after
      public int Value { get; set; }
      public void OnGet()
      {
      }
    
      public void OnPost()
      {
          int a = Value;
      }
    

*in page

<form method="post" asp-action="Index">
<input asp-for="Value" id="myVal" />

<input type="submit" value="send" />
</form>

<script>
 document.getElementById("myVal").value = 11;
</script>
Related