温馨提示:本文翻译自stackoverflow.com,查看原文请点击:asp.net - cshtml aspfor getter not working but the setter

asp.net - cshtml aspfor getter不起作用,但是setter

发布于 2020-03-27 10:54:51

我尝试通过asp-for从cshtml页面到cshtml.cs页面获取输入数据。

cshtml

<div class="form-group">
    <label class="col-sm-2 control-label">Name</label>
    <div class="col-sm-10">
        <textarea name="InputNameEvent" asp-for="NameEvent" class="form-control" placeholder="Name"></textarea>
    </div>
</div>

cshtml.cs

[Required]
[MinLength(5)]
[MaxLength(1024)]
public string NameEvent { get; set; }

public string Name2;

public void OnGet()
{
  NameEvent = "Test";
}
 public void OnPost()
 {
     Name2 = NameEvent;
 }

测试在Textarea中显示,但在OnPost()中NameEvent为NULL

查看更多

查看更多

提问者
Kai
被浏览
75
Kai 2019-07-04 18:19

我发现了我的错误。我需要放下<form method="post"></form>div。另外,我需要添加[BindProperty]cshtml.cs并删除cshtml中的name属性。感谢迈克:)

cshtml

<form method="post">
<div class="form-group">
    <label class="col-sm-2 control-label">Name</label>
    <div class="col-sm-10">
        <textarea asp-for="NameEvent" class="form-control" placeholder="Name"></textarea>
    </div>
</div>
</form>

cshtml.cs

[Required]
[MinLength(5)]
[MaxLength(1024)]
[BindProperty]
public string NameEvent { get; set; }