Bind SFGrid to AspNetCore.Identity.IdentityUser

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)

...



5 Replies

MC Mason Channer November 10, 2021 07:31 PM UTC

SfGrid DataSource=@AllUsers TValue="User"


I think you are missing TValue on the datagrid component



MA Marc November 10, 2021 09:31 PM UTC

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.





VN Vignesh Natarajan Syncfusion Team November 11, 2021 06:01 AM UTC

Hi Marc,  

Greetings from Syncfusion support.  

Query: “What I would like to do is see a list of users from identity server.  I am getting the error as shown below. 

We have analyzed your query and we are able to find the cause of the issue. Mason, thanks for your suggestion. You are partially correct. The issue occur due to TValue property of GridEvents not the SfGrid’s. We would like to inform you the TValue of GridEvents and SfGrid must be same and it must match with the Grid datasource type. (i.e.) If Grid datasource is of List<T>, then GridEvents and SfGrid’s  TValue must be T.  

When DataSource property is defined it is not necessary to TValue externally, we will fetch the type from datasource itself. For GridEvents alone we need specify the TValue. From your code example we found that you have bound List<IdentityUser> to Grid. So TValue must be Identityuser.  

Refer the below code example.  

AllUsers = new List<IdentityUser>();
 
@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> 


Please get back to us if you have further queries.  

Regards, 
Vignesh Natarajan  



MA Marc November 11, 2021 11:51 AM UTC

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.



VN Vignesh Natarajan Syncfusion Team November 12, 2021 05:11 AM UTC

Hi Marc,  

Thanks for the update. 

We are glad to hear that you have resolved your query using our solution.  

Please get back to us if you have further queries.  

Regards, 
Vignesh Natarajan 



Loader.
Up arrow icon