简体   繁体   中英

asp.net core razor pages onpost set value of textbox

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();
        }



    }
}

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM