asp.net核心剃须刀页面onpost设置文本框的值

时间:2020-07-29 00:17:15

标签: asp.net asp.net-core

我是asp.net核心的新手,我正在使用基本的剃刀页面并尝试通过Submit按钮发布onpost,然后将绑定到该页面的公共属性从onpost设置为特定值。这是我的下面的代码。但是,当我单击“提交”按钮时,该帖子发生了,但是没有任何更新?当在OnPost中将test1设置为“这应该可以工作”时,如何回到文本框中的页面上?

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>

然后我的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 个答案:

答案 0 :(得分:1)

但是当我单击“提交”按钮时,该帖子发生了,但没有 得到更新?在OnPost中将test1设置为 回到文本框页面的“此方法应该起作用”吗?

要解决此问题,您应该将值属性绑定到输入控件,如下所示:

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

您可以参考此link

这是测试结果:

enter image description here