An example for datagrid for vanilla EF Core MVC and API Scaffolding

Hi ,

I am trying to use the datagrid from syncfusion.ej2.aspnet.core(18.2.0.59). 

In setting up my application I use the 'vanilla' scaffolding for mvc with entity framework views and actions provided by Microsoft and also alternatively the scaffolding with entity framework web api  actions template

Is there any easy way of using the 'vanilla crud code' created by their scaffolding with your datagrid urladaptor  or webapi adaptor for the data grid.

Almost all examples in this forum use code in the controller that is dramatically changed from the Entity Framework vanilla generated code.

I did try and use the Syncfusion scaffolding template and it works but  the controller and view generated concerns itself only with the presentation of the database into populating the grid. And yes that works flawlessly, as do any of the examples of simply populating the grid. The background  plumbing of the CRUD actions never show the use of the EF scaffolding  vanilla generated code for create update and delete actions.

Am I missing something here? Syncfusion datagrid and other controls are very slick and professional looking and also in terms of how they behave. But if i have to hugely change the code in the ef scaffold generated controller to accomodate the use of the CRUD features, it defeats the purpose.

In the past I have used jquery datables as my grid, because they have allowed me to use the scaffolded code - close to its vanilla form. But the syncfusion controls are VERY  good, so I would prefer to use them.

Can examples be provided in C# for the use of the datagrid both in an mvc and web api scenario that use the vanilla  code created by the EF scafffolding for the crud operations.

Sorry if there are already examples on Syncfusion's forum- but I havent found the above to date after searching for a few days...

Thanks


5 Replies 1 reply marked as answer

MS Manivel Sellamuthu Syncfusion Team October 12, 2020 02:23 PM UTC

Hi Lyndon, 

Sorry for the late reply. 

Based on your requirement we found that you need a sample in Data Grid with built-in Entity Framework CRUD operations.  

Currently we are working on the sample but we face some complexity in the sample. So, we will provide you the sample by tomorrow 13th October, 2020.   

If we misunderstood your requirement, please get back to us. 

Regards, 
Manivel 



LY Lyndon October 19, 2020 06:26 PM UTC

Any update on this? Thanks


MS Manivel Sellamuthu Syncfusion Team October 26, 2020 01:56 PM UTC

Hi Lyndon, 
 
Sorry for the delay getting back to you. 
 
We have prepared a sample on Data Grid with built-in Entity Framework CRUD operations. Please find the below sample for more information.  
 
 
In this sample to perform server side crud operations we used UrlAdaptor. Please refer the below documentation for more information. 
 
 
Please let us know, if you need further assistance. 
 
Regards, 
Manivel 


Marked as answer

LY Lyndon October 27, 2020 03:25 AM UTC

Hi, when converting my app to your equivalent included example code- everything works except for add./insert which results in an 'object reference not set to an instance of an object'

The 'param' has a  value of null.



Your code example works for add/insert- but I have to put in the value of the key column to make it work.


My App uses a sql table where the key id column is autogenerated in SQL Server itself as an identity column.


The contoller code is as follows:

public ActionResult Normal_Insert([FromBody] CRUDModel<Prdsrv> param)
        {


            string connectionString = @"Server=WIN10DESKTOP\VS2019;Database=WorkManagerV4;Trusted_Connection=True;"; 
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SET IDENTITY_INSERT PRDSRV ON";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "INSERT INTO PRDSRV(PRODSERVID,PRODSERVDESC,LASTUPDATED,SERVICESHORTDESC,SERVICEDESC)   VALUES(@PRODSERVID,@PRODSERVDESC,@LASTUPDATED,@SERVICESHORTDESC,@SERVICEDESC)";
            cmd.Parameters.AddWithValue("@PRODSERVID", param.Value.Prodservid);
            cmd.Parameters.AddWithValue("@PRODSERVDESC", param.Value.Prodservdesc);
            cmd.Parameters.AddWithValue("@LASTUPDATED", DateTime.Now);
            cmd.Parameters.AddWithValue("@SERVICESHORTDESC", param.Value.Serviceshortdesc);
            cmd.Parameters.AddWithValue("@SERVICEDESC", param.Value.Servicedesc);

            cmd.ExecuteNonQuery();
            con.Close();
            return Json(param.Value);

        }

the index cshtml is as follows

    <ejs-grid id="Grid"  allowPaging="true" allowSorting="true" allowFiltering="true" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update","Cancel" })">
        <e-data-manager url="/Prdsrvs/UrlDataSource" adaptor="UrlAdaptor" ></e-data-manager>
    <e-data-manager url="@Url.Action("UrlDatasource", "Prdsrvs")" adaptor="UrlAdaptor" InsertUrl="/Prdsrvs/Normal_Insert" updateUrl="/Prdsrvs/Normal_Update" removeUrl="/Prdsrvs/Normal_Delete"></e-data-manager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog"> </e-grid-editSettings>
    <e-grid-pagesettings pageSize="10"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="Prodservid" headerText="Product Service ID" textAlign="Right" width="100" isPrimaryKey="true" isIdentity="true"></e-grid-column>
        <e-grid-column field="Prodservdesc" headerText="Product Service Description" width="300"></e-grid-column>
        <e-grid-column field="Lastupdated" headerText="Last Updated" width="300"></e-grid-column>
        <e-grid-column field="Serviceshortdesc" headerText="Short Description" width="300"></e-grid-column>
        <e-grid-column field="Servicedesc" headerText="Service Description" width="300"></e-grid-column>

    </e-grid-columns>
</ejs-grid>

Obviously, i would rather have the sql table take care of the incrementing the key value

What mistake am I making? thanks


MS Manivel Sellamuthu Syncfusion Team October 28, 2020 01:29 PM UTC

Hi Lyndon, 

Thanks for your update. 
 
We checked your reported problem with the shared code snippet from our end. 
 
The reported problem was reproduced in our end and it was occurring because when ‘isIdentity’ is enabled for the primary key column, the Grid will send ‘null’ value for this column on performing add operation(considering this value will be generated in the data base). Now as the Insert CRUD method in the .cs file expects an integer value for the primary key data field(Prodservid) and ‘null’ value is sent for this case, the values will not be properly received in the CRUD method arguments. 
 
So you can resolve this by giving nullable type for this primary key field in the Model definition. 
 
 
Please let us know, if you need further assistance. 
 
Regards, 
Manivel 


Loader.
Up arrow icon