0
votes

i am new to asp.net core, i am working with basic razor page and trying to issue onpost via submit button , then have a public property that is binded to the page get set to a specific value from the onpost. here is my code below. But when i click on the submit button the post happens but nothing gets updated? how can i get the test1 when it's set in the OnPost to "this should work" back on the page in the textbox ?

Index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<form method="post">
    <label>testing</label>
    <input asp-for="@Model.test1" type="text" />
    <br />
    <br />
    <input id="Submit1" type="submit" value="submit" />

</form>

then my Index.cshtml.cs

namespace WebApplication1.Pages
{
    [BindProperties]
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

     
        public string test1 { get; set; }

        public void OnGet()
        {
            
         
        }

        public IActionResult OnPost()
        {

            test1 = "this should work";

            return Page();
        }



    }
}
1

1 Answers

3
votes

But when i click on the submit button the post happens but nothing gets updated? how can i get the test1 when it's set in the OnPost to "this should work" back on the page in the textbox ?

To solve this issue, you should bind value attribute to input control as follow:

 <input asp-for="@Model.test1" type="text"  value="@Model.test1"/>

And you can refer to this link.

Here is the test result:

enter image description here