How to submit a form with a bound grid

I have an object that has a collection as a member:

Something like:
public class Kid
{
  public string Name;
  public Int32 Age;
}

public class Parent
  public string Name;
  public List<Kid> Kids;
}

I was trying to use the Html.BeginForm and pass in the Parent object, which works, until I start adding a Kid.  

I'd like to be able to just edit the Kids collection via a grid and have it passed in with the rest of the form....but I'm hitting all sort of problems which makes me think I'm missing something basic.

Do you have a simple example somewhere?

Thank you,

Gene





5 Replies 1 reply marked as answer

GA Gene Allen February 11, 2021 03:12 PM UTC

The grid looks something like this: 

<ejs-grid id="KidsGrid" dataSource="@Model.Kids" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })">
     <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" newRowPosition="Bottom"></e-grid-editSettings>
     <e-grid-columns>
          <e-grid-column field="Name" headerText="Name"  width="25"></e-grid-column>
          <e-grid-column field="Age" headerText="Age" validationRules="@(new { required=true, number=true})" width="25"></e-grid-column>
     </e-grid-columns>
</ejs-grid>




GA Gene Allen February 11, 2021 04:13 PM UTC

maybe the problem is that Microsoft.AspNetCore.Mvc.NewtonsoftJson package isn't compatible with .net core 3.1
<sigh>


Error NU1202 Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 5.0.3 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 5.0.3 supports: net5.0 (.NETCoreApp,Version=v5.0) FrestTest C:\Users\allengb.BYSTORM\source\repos\FrestTest\FrestTest\FrestTest.csproj 1




GA Gene Allen February 11, 2021 10:46 PM UTC

Still fighting this.  

finally decided to go with a serialized json and updated a hidden input on the form.

    var ds = document.getElementById("KidsGrid").ej2_instances[0].dataSource;
    var s = JSON.stringify(ds);
    $("#StringData").val(s);


and it ALMOST works, except the .dataSource doesn't reflect any changes that I make.  

This shouldn't be this hard.


GA Gene Allen February 11, 2021 10:58 PM UTC

ahh...I think I found the problem with the last approach.  

The magic bit that I was missing what the primary key (since I'm doing a full drop/rebuild method, I don't need a primary key)

<e-grid-column field="RowId" isPrimaryKey="true" visible="false"></e-grid-column>

Anyway, I added the line above and now the dataSource is changing.

Marked as answer

PG Praveenkumar Gajendiran Syncfusion Team February 12, 2021 09:00 AM UTC

Hi Gene, 
Greetings from Syncfusion support.

By default in EJ2 Grid editing feature requires a primary key column for CRUD operations. Because all the CRUD actions in the grid component are done based on the primarykey. Please refer the below document for more information.

Documentation: https://ej2.syncfusion.com/aspnetcore/documentation/grid/edit/ 

We are glad that you have resolved the reported problem at your end. 
Please get back to us if you need further assistance. 

Regards, 
Praveenkumar G 


Loader.
Up arrow icon