Blazorise: Accessing an item within a list

Created on 15 Apr 2020  路  5Comments  路  Source: stsrki/Blazorise

Hey.
I'm trying to display an item that is in a list of the model, but everything i've tried doesn't seem to work.

<DataGridColumn TItem="Student" Field="@nameof(Student.Name.Text)" Caption="Name"></DataGridColumn>

Does not work. _Student.Name_ is a list and _Text_ is one of the items.

I've tried
Field=Name.Text
with no success.

Any suggestions?

Question

Most helpful comment

I think the issue here is that Name is a list, and there is no way of telling what item from the list you want.

If you want to keep it as a list, I would suggest using a custom DisplayTemplate.
Something like the following:

<DataGridNumericColumn TItem="Student" Field="@nameof(Student.Name)" Caption="Name">
    <DisplayTemplate>
        @context.Name.First().Text
    </DisplayTemplate>
</DataGridNumericColumn>

But @stsrki might have a better suggestion!

All 5 comments

When you use nameof(Student.Name.Text) it will get only the Text and then it will try to get that field name from Student object.

Try with Field="Student.Name.Text".

That doesn't work either.
I get this exception
_ArgumentException: 'Student' is not a member of type 'Student' (Parameter 'propertyOrFieldName')_

The model is structured like this:

public class HumanName
{
public string Text {get; set;};
}

public class Student
{
public List<HumanName> Name {get; set;};
}

if that helps

I think the issue here is that Name is a list, and there is no way of telling what item from the list you want.

If you want to keep it as a list, I would suggest using a custom DisplayTemplate.
Something like the following:

<DataGridNumericColumn TItem="Student" Field="@nameof(Student.Name)" Caption="Name">
    <DisplayTemplate>
        @context.Name.First().Text
    </DisplayTemplate>
</DataGridNumericColumn>

But @stsrki might have a better suggestion!

That worked like a charm! Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Andy74 picture Andy74  路  4Comments

DDDenisSobek picture DDDenisSobek  路  4Comments

imtrobin picture imtrobin  路  4Comments

stsrki picture stsrki  路  3Comments

stsrki picture stsrki  路  3Comments