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. Image for the cookie policy date

MongoDB.Bson.ObjectId as a primary key in Grid

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-columns>
        <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 ... >
</e-columns>
</ej-grid>

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.



1 Reply

VA Venkatesh Ayothi Raman Syncfusion Team 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-columns> 
        <e-column field="id.employID" is-primary-key="true" header-text="ID" width="90"></e-column> 
            . . . 
        </e-column> 
         
 
    </e-columns> 
</ej-grid> 
 
@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? 
Regards, 
Venkatesh Ayothiraman. 


Loader.
Up arrow icon