We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

MongoDB.Bson.ObjectId as a primary key in Grid

Thread ID:





129871 Apr 6,2017 03:15 PM UTC Apr 7,2017 12:43 PM UTC ASP.NET Core 1
Tags: Grid
Valer Bocan
Asked On April 6, 2017 03:15 PM UTC

My experiments with the Grid control revealed that the primary key must be either an integer or a string. I would like to use the grid to display objects stored in a MongoDB database, having the primary keys of type ObjectId, as follows:

public class ClientModel
    public ObjectId Id { get; set; }
  public string Name { get; set; }
  ... some other fields ...

The grid definition in the .cshtml file is as follows:

<ej-grid id="ClientGrid" allow-paging="true">
    <e-datamanager json="(IEnumerable<object>)ViewBag.DataSource" remove-url="@Url.Action("NormalDelete")" adaptor="remoteSaveAdaptor" />
    <e-edit-settings allow-deleting="true" edit-mode="@(EditMode.Normal)"></e-edit-settings>
    <e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() { "add", "edit", "delete", "update", "cancel" })"></e-toolbar-settings>

        <e-column field="Id" header-text="Client ID" is-primary-key="true" text-align="Right" width="75"></e-column>
        <e-column field="Name" header-text="Name"></e-column>
                < ... some other columns ... >

The exception I get upon displaying the grid is:

Object doesn't support property or method 'replace'

and the stack trace is:

t.gridFeatures.edit._htmlEncode [ej.web.all.min.js]           Line 10
  t.gridFeatures.edit._getPrimaryKeyValues [ej.web.all.min.js]  Line 10
  t.gridFeatures.edit.deleteRow [ej.web.all.min.js]             Line 10
  t.gridFeatures.edit._toolbarOperation [ej.web.all.min.js]     Line 10

The workaround I have found is to add a computed property to ClientModel and return the Id as a string on the fly. This is more like a hack and I was wondering whether there was an alternative.

Venkatesh Ayothi Raman [Syncfusion]
Replied On April 7, 2017 12:43 PM UTC

Hi Valer, 
Thanks for contacting Syncfusion support. 
We have achieved your requirement using built-in support of complex binding feature in Grid. In that, we have defined the primary column field as complex binding like as follows, 
Code example
<ej-grid id="FlatGrid" datasource="@ViewBag.dataSource" allow-paging="true" > 
    . . . 
        <e-column field="id.employID" is-primary-key="true" header-text="ID" width="90"></e-column> 
            . . . 
@server side model class 
public class Orders 
            public Orders() 
. . . 
            public ObjectID id { get; set; } 
            . . . 
        public class ObjectID 
            public string Name { get; set; } 
            public int employID { get; set; } 
            public ObjectID(int id, string name) 
                this.Name = name; 
                this.employID = id; 
Note: here we can return the id without covert this as string 
We have also prepared a sample for your convenience which can be download from following link, 

If you still face the same issue, then could you please provide more details about your requirement or modified the given sample as issue reproducible? 
Venkatesh Ayothiraman. 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon