use*_*480 8 c# asp.net asp.net-core blazor-server-side asp.net-core-3.0
我正在构建一个示例 razor 组件,但我的按钮 onclick 方法无法触发。当我单击按钮时,什么也没有发生。我什至在方法中放置了一个断点,以查看它是否捕获了它没有捕获的。该组件确实呈现,但正如我所说,单击按钮时,LoginUser 方法根本没有运行。
剃须刀组件页面:
<div class="text-center">
<Login FieldsetAttr="fieldsetAttr" UsernameAttr="usernameAttr" PasswordAttr="passwordInput"
ButtonAttr="buttonAttr" ButtonText="Sign In" SignInManager="SignInManager"
InvalidAttr="invalidAttr" />
</div>
@code {
Dictionary<string, object> fieldsetAttr =
new Dictionary<string, object>()
{
{"class", "form-group" }
};
Dictionary<string, object> usernameAttr =
new Dictionary<string, object>()
{
{"class", "form-control" },
{"type", "text" },
{"placeholder", "Enter your user name here." }
};
Dictionary<string, object> passwordInput =
new Dictionary<string, object>()
{
{"class", "form-control" },
{"type", "password" }
};
Dictionary<string, object> buttonAttr =
new Dictionary<string, object>()
{
{"class", "" },
{"type", "button" }
};
Dictionary<string, object> invalidAttr =
new Dictionary<string, object>()
{
{"class", "invalid-feedback" }
};
}
Run Code Online (Sandbox Code Playgroud)
剃须刀组件:
<div @attributes="FormParentAttr">
<form @attributes="LoginFormAttr">
<fieldset @attributes="FieldsetAttr">
<legend>Login</legend>
<label for="usernameId">Username</label><br />
<input @attributes="UsernameAttr" id="usernameId" @bind="UserName" /><br />
<label for="upasswordId">Password</label><br />
<input @attributes="PasswordAttr" id="passwordId" @bind="Password" /><br />
<button @attributes="ButtonAttr" @onclick="LoginUser">@ButtonText</button>
@if(errorMessage != null && errorMessage.Length > 0)
{
<div @attributes="InvalidAttr">
@errorMessage
</div>
}
</fieldset>
</form>
</div>
@code {
[Parameter]
public Dictionary<string, object> FormParentAttr { get; set; }
[Parameter]
public Dictionary<string, object> LoginFormAttr { get; set; }
[Parameter]
public Dictionary<string, object> FieldsetAttr { get; set; }
[Parameter]
public Dictionary<string, object> UsernameAttr { get; set; }
[Parameter]
public Dictionary<string, object> PasswordAttr { get; set; }
[Parameter]
public Dictionary<string,object> ButtonAttr { get; set; }
[Parameter]
public SignInManager<IdentityUser> SignInManager { get; set; }
[Parameter]
public Dictionary<string, object> InvalidAttr { get; set; }
private string UserName { get; set; }
private string Password { get; set; }
[Parameter]
public string ButtonText { get; set; }
private string errorMessage { get; set; }
private async Task LoginUser(MouseEventArgs e)
{
var user = new IdentityUser(UserName);
var loginResult = await SignInManager.CheckPasswordSignInAsync(user, Password, true);
if(loginResult.Succeeded)
{
await SignInManager.SignInAsync(user, true);
errorMessage = "";
}
else
{
errorMessage = "Username or password is incorrect.";
}
}
}
Run Code Online (Sandbox Code Playgroud)
Rya*_*anY 14
这发生在我的 MVC 核心项目中,我试图按照此博客的指导添加 blazor 组件。在将我的项目与模板 blazor 项目进行比较后,我发现是丢失的_Imports.razor
文件导致 blazor.server.js 无法订阅点击事件。我将该文件添加到我的项目中,就像在模板项目中一样:
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
Run Code Online (Sandbox Code Playgroud)
然后按钮点击按预期工作。
小智 5
确保你
在public void ConfigureServices(IServiceCollection services)
您调用中services.AddServerSideBlazor();
(...对于服务器端 Blazor)
并public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
注册端点,例如(“ endpoint.MapBlazorHub() ”是您需要在此处添加的内容):
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
Run Code Online (Sandbox Code Playgroud)
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Web
Run Code Online (Sandbox Code Playgroud)
在您使用 Blazor 的 Razor 组件中。
归档时间: |
|
查看次数: |
5251 次 |
最近记录: |