使用Razor页面更新帖子标签 [英] Update label on post using Razor Pages

查看:71
本文介绍了使用Razor页面更新帖子标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新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>

  1. 我不希望在视图中显示确认"一词,而是要显示设置已保存!"属性的设置值.
  2. 此外,当执行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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆