使用Razor页面更新帖子标签 [英] Update label on post using Razor Pages
问题描述
我正在尝试更新Razor页面中http post
上的标签.我有一个名为Confirmation
的公共财产,如下所示:
I'm trying to update a label on an http post
within a Razor Page. I have a public property called Confirmation
as follows:
public string Confirmation { get; set; }
然后我有我的post
方法:
public async Task<IActionResult> OnPostAsync()
{
//some conditional logic here
Confirmation = "Settings saved!";
return Page();
}
我认为我有:
<label asp-for="Confirmation"></label>
但是,它呈现为:
<label for="Confirmation">Confirmation</label>
- 我不希望在视图中显示确认"一词,而是要显示设置已保存!"属性的设置值.
- 此外,当执行post方法并设置string属性时,它不会更新标签的文本.
我尝试将OnGetAsync()
方法中的Confirmation
属性设置为",但这仍会导致标签上显示相同的文本.
I've tried setting the Confirmation
property in the OnGetAsync()
method to "" but this still results in the same text displaying on the label.
推荐答案
您应该尝试<label asp-for="Confirmation">@Model.Confirmation</label>
.
在旁注中,我怀疑您在滥用label元素.它旨在与同名的表单字段配对,并呈现HTML标签. Win Forms和Web Forms使用<label>
控件作为输出文本的通用占位符并没有帮助.在Web Forms中,它呈现为<span>
,这可能更适合(尽管您不会获得标签帮助程序帮助).
On a side note, I suspect that you are misusing the label element. It is intended to be paired with a form field of the same name and renders an HTML label. It doesn't help that Win Forms and Web Forms use the <label>
control as an all-purpose placeholder for outputting text. In Web Forms it renders as a <span>
, which might be more suitable (although you won't get the tag helper assistance).
这篇关于使用Razor页面更新帖子标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!