【问题标题】:Change button label text dynamically in razor pages在剃刀页面中动态更改按钮标签文本
【发布时间】:2019-12-21 06:14:34
【问题描述】:

我在我的 ASP.NET Core Web 应用程序中使用 Razor Pages。

我想从代码隐藏中更改按钮文本。即使我在 Html 标记中添加 runat="server" 并且我无法访问 *.cshtml.cs 文件中的按钮。

另外,我不能在剃须刀页面中使用<asp:button>。不使用javascript方法有什么办法吗?

<input type="submit" value="Create" class="btn btn-primary"/>

MyPage.cshtml.cs

public IActionResult OnGet(Guid id)
{
if(id == Guid.Empty) //Make button create
else // make it update
}

【问题讨论】:

  • 您是否要更改按钮上的标签?
  • @Greg:是的。根据查询字符串,它应该是 Create 或 Update。
  • 你应该可以使用@语法&lt;input type="submit" value="@PropertyName" class="btn btn-primary"/&gt;将它绑定到你的cs代码中的一个属性上
  • 您可以使用TempDataViewBag@
  • 对!感谢您的建议。

标签: c# asp.net-core razor-pages


【解决方案1】:

.net 核心中的 Razor 页面与经典的 aspx 页面有很大不同。这个one from MS 中有很多很好的介绍文章。使用您的问题和该文章中的一些示例代码,页面可能看起来像这样。

cshtml 文件可能如下所示:

@page
@using RazorPagesIntro.Pages
@model IndexModel2

<h2>Separate page model</h2>
<p>
    <input type="submit" value="@Model.Label" class="btn btn-primary"/>
</p>

cshtml.cs 页面模型:

using Microsoft.AspNetCore.Mvc.RazorPages;
using System;

namespace RazorPagesIntro.Pages
{
    public class IndexModel2 : PageModel
    {
        [BindProperty]
        public string Label { get; set; }

        public void OnGet()
        {
            if (id == Guid.Empty) {
                Label = "Create"; 
            } else {
                Label = "Update";
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多