Hi.
I have implemented this authentication in a blazor server project. What I would like to do is see a list of users from identity server. I am getting the error as shown below.
I feel like I am close. The Allusers object is fully populated when I step through the code. Any thoughts on this would be appreci
I have done this with the following query in a CS module.
public void GetUsers()
{
// Collection to hold users
AllUsers = new List<IdentityUser>();
// get users from _UserManager
var userIDS = _UserManager.Users.Select(x => new IdentityUser
{
Id = x.Id,
UserName = x.UserName,
Email = x.Email,
PasswordHash = "*****"
}).OrderBy(x => x.Email);
//Populate the list object
foreach (IdentityUser item in userIDS)
{
AllUsers.Add(item);
}
}
Page initializes with this.
protected override async Task OnInitializedAsync()
{
// Get the current logged in user
CurrentUser = (await authenticationStateTask).User;
GetUsers();
}
On the razor page side.
<SfGrid DataSource=@AllUsers class="mr-2" Width="1200"
AllowPaging="true"
AllowFiltering="false"
AllowSorting="true"
AllowTextWrap="true"
AllowResizing="true"
AllowGrouping="false"
RowHeight="30"
Toolbar="@(new List<string>() {"Edit", "Delete", "Cancel", "Update" })">
<GridEditSettings AllowAdding="true"
AllowDeleting="true"
AllowEditing="true"
AllowEditOnDblClick="true"
Mode="EditMode.Normal"
AllowNextRowEdit="false"
ShowConfirmDialog="true"
ShowDeleteConfirmDialog="true"/>
<GridPageSettings PageSize="40"></GridPageSettings>AllUsers
@*<GridEvents OnRecordDoubleClick="RecordDoubleClickHandler" TValue="FileTemplateHeader"></GridEvents>*@
<GridEvents OnActionBegin="ActionBeginHandler" OnActionComplete="ActionCompleteHandler" TValue="User"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(objUser.Id) HeaderText="ID" IsPrimaryKey="true" LockColumn="true" AllowAdding="false" TextAlign="TextAlign.Left" MinWidth="10" Width="35" />
</GridColumns>
</SfGrid>
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
Syncfusion.Blazor.Grids.GridEvents<TValue>.OnInitializedAsync()
NullReferenceException: Object reference not set to an instance of an object.
Syncfusion.Blazor.Grids.GridEvents<TValue>.OnInitializedAsync()
Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
Microsoft.AspNetCore.Components.Rendering.HtmlRenderer.HandleException(Exception exception)
...
SfGrid DataSource=@AllUsers TValue="User"
I think you are missing TValue on the datagrid component
You are correct. Interestingly I cannot seem to find a correct Tvalue to use.
Earlier
in the code I made the following declaration; and it is this I use as a
TValue thinking that it will get its definition from there.
// Property used to add or edit the currently selected user
IdentityUser objUser = new IdentityUser();
However, if I change the SFGrid definition to below. I generate 2 new, but very telling errors.
<SfGrid DataSource=@AllUsers TValue="objUser" class="mr-2" Width="1200"
2 Errors:
DataSource=@AllUsers
Argument 1: cannot convert from
'System.Collections.Generic.List<Microsoft.AspNetCore.Identity.IdentityUser>' to 'System.Collections.Generic.IEnumerable<IdentityUser>' serverAuthTest.app
TValue=" objUser "
The type or namespace name 'IdentityUser' could not be found (are you missing a using directive or an assembly reference?) serverAuthTest.app
Clearly it is not liking
the TValue and that the objects type cannot be converted. Because I am
working with the aspnetcore.identity.usermanager object, I am struggling
on how to resolve this error.
@using Syncfusion.Blazor.Grids
<SfGrid DataSource=@AllUsers class="mr-2" Width="1200" AllowPaging="true" AllowFiltering="false" AllowSorting="true" AllowTextWrap="true"
AllowResizing="true"
AllowGrouping="false"
RowHeight="30"
Toolbar="@(new List<string>() {"Edit", "Delete", "Cancel", "Update" })">
<GridEditSettings AllowAdding="true"
AllowDeleting="true"
AllowEditing="true"
AllowEditOnDblClick="true"
Mode="EditMode.Normal"
AllowNextRowEdit="false"
ShowConfirmDialog="true"
ShowDeleteConfirmDialog="true" />
<GridPageSettings PageSize="40"></GridPageSettings>
<GridEvents OnActionBegin="ActionBeginHandler" OnActionComplete="ActionCompleteHandler" TValue="IdentityUser"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(objUser.Id) HeaderText="ID" IsPrimaryKey="true" LockColumn="true" AllowAdding="false" TextAlign="TextAlign.Left" MinWidth="10" Width="35" />
</GridColumns>
</SfGrid>
|
Thanks for the clarification Vignesh.
Just to step back to the final solution.
When I remove the <GridEvents> line on the razor page, the code above works.
When I get the list of Users, which is returned as a IOrderedQueryable, I cast that to a List. It could not be bound to directly as an IOrderedQueryable. So in the sample above @Allusers is defined as
public List<IdentityUser> AllUsers = new List<IdentityUser>();
public IdentityUser objUser;
This code works fine.
You are absolutely correct about the TValue ( and thank you for clarifying when this is required and where, this had been a little fuzzy for me) in that it does need to be set to IdentityUser.
There are two additional steps required to make this work.
#1 @using Microsoft.AspNetCore.Identity at the top of the razor page. In this case the CS code is contained in a seperate file as a partial class.
#2 In the CS file, ensure that event handlers are decllared with the same IdentityUser type. I missed that the first go around.
public void ActionBeginHandler(ActionEventArgs<IdentityUser> Args)
As always, thanks for you help. Much appreciated.
Marc.