【发布时间】:2021-07-07 16:11:29
【问题描述】:
我正在开发一个 ASP.NET Core (.NET 5.0) 服务器端 Blazor 应用程序。我正在尝试在模式中添加/编辑用户,并在另一个模式中为用户分配角色。所以,我有一个父组件和两个子组件。
父组件:
@if (ShowPopup)
{
<!-- This is the popup to create or edit user -->
<EditUserModal ClosePopup="ClosePopup" CurrentUserRoles="" DeleteUser="DeleteUser" objUser="objUser" SaveUser="SaveUser" strError="@strError" />
}
@if (ShowUserRolesPopup)
{
<!-- This is the popup to create or edit user roles -->
<EditUserRolesModal AddRoleToUser="AddRoleToUser" AllRoles="AllRoles" ClosePopup="ClosePopup" CurrentUserRoles="@CurrentUserRoles" objUser="objUser" RemoveRoleFromUser="RemoveRoleFromUser" SelectedUserRole="@SelectedUserRole" strError="@strError" _allUsers="_allUsers" _userRoles="UserRoles" />
}
@code {
// Property used to add or edit the currently selected user
ApplicationUser objUser = new ApplicationUser();
// Roles to display in the roles dropdown when adding a role to the user
List<IdentityRole> AllRoles = new List<IdentityRole>();
// Tracks the selected role for the current user
string SelectedUserRole { get; set; }
// To enable showing the Popup
bool ShowPopup = false;
// To enable showing the User Roles Popup
bool ShowUserRolesPopup = false;
......
}
父组件包含保存数据对象(状态)的对象,例如objUser、SelectedUserRole 等以及使用这些对象实现业务规则的方法,例如SaveUser 和 AddRoleToUser。
子组件EditUserModal用于添加/编辑用户。它将objUser 作为父组件的参数。 'objUser' 保存新/选定用户的值。
<div class="form-group">
<input class="form-control" type="text" placeholder="First Name" @bind="objUser.FirstName" />
</div>
<div class="form-group">
<input class="form-control" type="text" placeholder="Last Name" @bind="objUser.LastName" />
</div>
<div class="form-group">
<input class="form-control" type="text" placeholder="Email" @bind="objUser.Email" />
</div>
<button class="btn btn-primary" @onclick="SaveUser">Save</button>
@code {
[Parameter]
public ApplicationUser objUser { get; set; }
[Parameter]
public EventCallback SaveUser { get; set; }
.......
}
objUser 对象和SaveUser 事件回调是子组件中的参数。以子组件的形式输入数据并保存后,在父组件中调用SaveUser,自动与父组件中的objUser对象进行数据绑定。该对象保存当前输入的数据。然后使用该对象调用数据库服务来创建/更新用户数据。它运行完美!
看起来是这样的:
objUser 对象已输入数据,一切正常。
但 问题 是在 EditUserRolesModal 组件中,我在其中使用变量做一些类似的事情。 EditUserRolesModal 组件代码:
<h5 class="my-3">Add Role</h5>
<div class="row">
<div class="col-md-10">
<div class="form-group">
<input class="form-control" type="text" @bind="objUser.Email" disabled />
</div>
<div class="form-group">
<select class="form-control" @bind="@SelectedUserRole">
@foreach (var option in AllRoles)
{
<option value="@option.Name">@option.Name</option>
}
</select>
</div>
<button class="btn btn-primary" @onclick="AddRoleToUser">Assign</button>
</div>
</div>
@code {
[Parameter]
public ApplicationUser objUser { get; set; }
[Parameter]
public string SelectedUserRole { get; set; }
[Parameter]
public List<IdentityRole> AllRoles { get; set; }
[Parameter]
public EventCallback AddRoleToUser { get; set; }
........
}
这里的参数SelectedUserRole用于保存要分配给用户的角色的值。它绑定到下拉列表。当我更改下拉列表的值时,调试显示该值在子组件(参数)对象中更新。下面的图片展示了它:
值设置为子组件中的SelectedUserRole参数对象。
很像,objUser 在EditUserModal 中用于保存新/选定用户的字段FirstName、LastName 和Email 的值,SelectedUserRole 用于EditUserRolesModal 到保存要分配给用户的选定角色的值。
但是当Assign按钮被按下并通过子组件在父组件中调用AddRoleToUser事件时,SelectedUserRole变量在父组件中包含null。
两个子组件中的场景是一样的,目的是当子组件内部的参数值发生变化时,绑定的值应该更新并反映在父组件的状态中。但是,它适用于 EditUserModal 组件,而不适用于 EditUserRolesModal 组件。为什么?我做错了什么?
附:我在 Blazor 或基于组件的 Web 编程方面没有经验。
编辑:我已经为这个项目创建了一个公共存储库。它还包含数据库脚本文件。可以找到here。
【问题讨论】:
-
我没有非常仔细地分析你的代码,但是.. 你为什么不把你的
ApplicationUser逻辑代码(CRUD、更新角色等)放在一个地方 - 一个数据服务对象。您可以通过依赖注入从所有组件中访问这一事实副本。如果您想跟进,我会发布一些示例代码作为答案。 -
@MrCakaShaunCurtis 谢谢。我能做到。无需为它发布示例代码。现在,我需要知道我的问题的答案。
-
我添加了上面的评论作为答案。如果这解决了问题,请勾选它作为关闭问题的答案。
标签: asp.net asp.net-core blazor blazor-server-side asp.net-blazor