【发布时间】:2020-05-16 08:14:46
【问题描述】:
如何在 Razor 页面中运行的 Razor 组件中触发事件?
我的创业:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
}
调用组件的我的 Razor 页面:
@page
@model DocketDetail.OrderModel
@{
Layout = null;
}
@using RelationalObjectLayerCore;
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/lib/jquery/dist/jquery.js"></script>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="_framework/blazor.server.js"></script>
<script src="~/js/site.js"></script>
<title>Order</title>
</head>
<body>
<component type="typeof(Component.Filedby)" render-mode="ServerPrerendered" />
</body>
一切正常显示。
我的组件:
@using Microsoft.AspNetCore.Components
@code {
private void SearchPerson()
{
string x = "TEST";
}
}
<button @onclick="SearchPerson">Search</button>
显然这是从我的实际代码中缩减的......但我无法弄清楚如何让“SearchPerson”在 Razor 组件中触发。
【问题讨论】:
-
如果在浏览器中按F12,控制台是否有任何错误?
-
另外,尝试使用
@(await Html.RenderComponentAsync<Filedby>(RenderMode.ServerPrerendered))而不是<component type="typeof(Component.Filedby)" render-mode="ServerPrerendered" />渲染您的组件 -
我们在 razor 页面中广泛使用 Blazor,它运行良好。我能看到的唯一另一个明显区别是我们使用
RenderMode.Server而不是RenderMode.ServerPrerendered,并且我们在脚本声明中首先使用了<script src="_framework/blazor.server.js"></script>。
标签: razor-pages blazor blazor-server-side .net-core-3.1