asp-for标签助手未写入字段

时间:2020-03-31 16:53:34

标签: asp.net-core razor .net-core

asp-for标记不会写入我的字段,如果我将received_text设置为文本,它将显示在网页中。 表单确实发送数据,但是received_text始终为空。

CS:

public class IndexModel : PageModel
{
    public string received_text;

    public void OnGet()
    {
        Console.WriteLine("Text received = " + received_text);
    }
}

CShtml:

@page "/"

@model WebApplication2.HomePage.IndexModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<p>Send text</p>
<form action="/" method="get">
    <input asp-for=received_text />
</form>

<p>@Model.received_text</p>

1 个答案:

答案 0 :(得分:1)

您必须应用BindProperty属性,并且它必须是属性,而不是字段:

[BindProperty]
public string received_text { get; set; }