eddie_cat
eddie_cat

Reputation: 2583

How to display GUID in Kendo Grid?

I just want to display a GUID from my model in a Kendo grid in the view, but I am getting this error each time:

The model item passed into the dictionary is of type 'System.Guid', but this dictionary requires a model item of type 'System.String'.

This is my grid code:

@(Html.Kendo().Grid(Model.revisions)
                    .Name("RevisionsGrid")
                    .Columns(columns =>
                    {
                        columns.Bound(p => p.RevisionInfo.Id).Title("Revision ID");
                        columns.Bound(p => p.Accident.Id).Title("Accident ID")
                            .ClientTemplate("#= (Accident.Id == null) ? '' : Accident.Id.toString() #");
                        columns.Bound(p => p.RevisionInfo.RevisionDate).Title("Date Modified");
                        columns.Bound(p => p.RevisionInfo.User.Name).Title("By User");
                        columns.Command(command => command.Custom("ViewPdf").Text("View PDF").Click("getPdf"));
                    })
                    .Selectable()
                    .Pageable(p => p.PageSizes(new[] { 5, 10, 25 }))
                    .DataSource(dataSource => dataSource
                    .Server()
                    .Model(model =>
                        {
                            model.Id(p => p.RevisionInfo.Id);
                            model.Field(p => p.Accident.Id).DefaultValue(Guid.NewGuid());
                        }))

The problem is with the second column which is bound to Accident.Id, a GUID type. My attempts at fixing this included adding the ClientTemplate to that column & adding a default value for it, but neither of these helped. I also tried just adding ToString() in the column definition but then I get a different error about .Bound() only taking a property access method as an argument.

Is there some place to convert the GUID to a string in order to display it here?

Upvotes: 1

Views: 1711

Answers (1)

eddie_cat
eddie_cat

Reputation: 2583

I figured it out. In order to have the Kendo grid accept something that's not a string you need to specify a template for the column, in which you can call ToString(). I couldn't call it in binding.

columns.Bound(p => p.Accident.Id).Title("Accident ID")
    .Template(p => p.Accident.Id.ToString());

Upvotes: 2

Related Questions