C# 在razor页面中动态更改按钮标签文本
我正在ASP.NET核心web应用程序中使用Razor页面 我想从代码隐藏更改按钮文本。即使我在Html标记中添加runat=server,并且无法访问*.cshtml.cs文件中的按钮 此外,我不能在剃须刀页面使用。有没有不使用javascript方法的方法C# 在razor页面中动态更改按钮标签文本,c#,asp.net-core,razor-pages,C#,Asp.net Core,Razor Pages,我正在ASP.NET核心web应用程序中使用Razor页面 我想从代码隐藏更改按钮文本。即使我在Html标记中添加runat=server,并且无法访问*.cshtml.cs文件中的按钮 此外,我不能在剃须刀页面使用。有没有不使用javascript方法的方法 <input type="submit" value="Create" class="btn btn-primary"/> NETCore中的Razor页面与经典的aspx页面有很大的不同。在这本书中有很多很好的介绍文章。使
<input type="submit" value="Create" class="btn btn-primary"/>
NETCore中的Razor页面与经典的aspx页面有很大的不同。在这本书中有很多很好的介绍文章。使用你的问题和那篇文章中的一些示例代码,可以了解页面的外观 cshtml文件可能如下所示:
@page
@using RazorPagesIntro.Pages
@model IndexModel2
<h2>Separate page model</h2>
<p>
<input type="submit" value="@Model.Label" class="btn btn-primary"/>
</p>
你想改变按钮上的标签吗?@Greg:是的。根据查询字符串,它应该是Create或Update。您应该能够使用@语法将它绑定到cs代码中的属性。您可以使用TempData、ViewBag和@。对!谢谢你的建议。
@page
@using RazorPagesIntro.Pages
@model IndexModel2
<h2>Separate page model</h2>
<p>
<input type="submit" value="@Model.Label" class="btn btn-primary"/>
</p>
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";
}
}
}
}