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>
答案 0 :(得分:1)
您必须应用BindProperty
属性,并且它必须是属性,而不是字段:
[BindProperty]
public string received_text { get; set; }