When send data from grid to controller ,it arrives with null

In Index.chtml
The Child Grid In View
 var ChildGrid1 = new Syncfusion.EJ2.Grids.Grid()
    {
        DataSource = new Syncfusion.EJ2.DataManager() { Url = "/TicketConfigurationPath/ConfigurationPathDatasource",
            BatchUrl = "/TicketConfigurationPath/BatchUpdate", Adaptor = "UrlAdaptor" },
        QueryString = "ID",
        EditSettings = new Syncfusion.EJ2.Grids.GridEditSettings() { AllowAdding = true, AllowEditing = true,
            ShowConfirmDialog = true, Mode = Syncfusion.EJ2.Grids.EditMode.Batch },
        Toolbar = new List<string>() { "Add", "Update", "Cancel" },
        //Aggregates = new List<Syncfusion.EJ2.Grids.GridAggregate> {
        //    new Syncfusion.EJ2.Grids.GridAggregate() {Columns =col} },
        AllowFiltering = true,
        AllowSorting = true,
        Columns = new List<Syncfusion.EJ2.Grids.GridColumn> {
new Syncfusion.EJ2.Grids.GridColumn(){ Field="ID", HeaderText="ID",DefaultValue=0, IsPrimaryKey=true, Width="120"},
new Syncfusion.EJ2.Grids.GridColumn(){ Field="DepartmentID", HeaderText=ResourcesWeb.PayrollResource.DepartmentFrom, 
    DataSource=(IEnumerable<object>)ViewBag.FromDepartmentList ,
                       ForeignKeyField="ID", ForeignKeyValue="Name", EditType="dropdownedit" ,Width="200" },
new Syncfusion.EJ2.Grids.GridColumn(){ Field="TargetDepartmentID", HeaderText=ResourcesWeb.PayrollResource.DepartmentTarget, 
    DataSource=(IEnumerable<object>)ViewBag.TargetDepartmentList ,
                       ForeignKeyField="ID", ForeignKeyValue="Name", EditType="dropdownedit" ,Width="200" },
new Syncfusion.EJ2.Grids.GridColumn(){ Field="PositionID", HeaderText=ResourcesWeb.PayrollResource.PositionFrom
, DataSource=(IEnumerable<object>)ViewBag.FromPositionList ,
                       ForeignKeyField="ID", ForeignKeyValue="PositionName", EditType="dropdownedit" ,Width="200" },
new Syncfusion.EJ2.Grids.GridColumn(){ Field="TargetPositionID", HeaderText=ResourcesWeb.PayrollResource.PositionTarget, 
    DataSource=(IEnumerable<object>)ViewBag.TargetPositionList ,
                       ForeignKeyField="ID", ForeignKeyValue="PositionName", EditType="dropdownedit" ,Width="200" },
}

    };

When I make the controller action 
BatchUpdate with CRUDModel<T>
 [HttpPost]
        public async Task<ActionResult> BatchUpdate([FromBody] CRUDModel<TicketConfigurationPathViewModel> ticketConfiguration)
        {

            return await Task.FromResult(Json(ticketConfiguration));
       
        }
 The Value of ticketConfiguration is null here

And when I make the controller action 
BatchUpdat with CRUDModel
[HttpPost]
        public async Task<ActionResult> BatchUpdate([FromBody] CRUDModel ticketConfiguration)
        {
           //code
            return await Task.FromResult(Json(ticketConfiguration));
       
        }

I Can not cast or convert added or changed list from List of Object to List of  TicketConfigurationPathViewModel in My Project.
And What is the best parameter Format to receive data from child grid to controller action ? 

7 Replies 1 reply marked as answer

AH Ahmed March 28, 2021 12:48 PM UTC

CRUDModel  here is Syncfusion.EJ2.Base.CRUDModel  
and CRUDModel <TicketConfigurationPathViewModel> is Syncfusion.EJ2.Base.CRUDModel <T>


VS Vignesh Sivagnanam Syncfusion Team March 29, 2021 01:20 PM UTC

Hi Ahmed 

Greetings from Syncfusion support 

Based on your query you want to use the BatchUrl to perform the CRUD operation in the child grid. So, we have prepared a sample using the Urladaptor with Batchurl. You can use the below code example to achieve your requirement. 

Please refer the below code example and sample for your reference, 


<ejs-grid id="DataGrid" allowPaging="true" load="load" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })"> 
  <e-data-manager url="/Home/UrlDatasource" batchUrl="/Home/BatchUpdate" adaptor="UrlAdaptor"></e-data-manager
  <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Batch" showConfirmDialog="false"></e-grid-editSettings
  <e-grid-columns
…………………………………………………………. 
  </e-grid-columns
</ejs-grid

<script> 

  function load(args) { 
      var grid = document.getElementById('DataGrid').ej2_instances[0] 
    grid.childGrid = { 
      dataSource: new ej.data.DataManager({ 
        url: "/Home/UrlDatasourceChild", 
        batchUrl: "/Home/BatchUpdateChild", 
        adaptor: new ej.data.UrlAdaptor() 
      }), 
      queryString: 'EmployeeID', 
      editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, allowUpdating: true, mode: 'Batch', showConfirmDialog: true }, 
      allowPaging: true, 
      toolbar: ["Add", "Edit", "Update", "Cancel", "Delete"], 
      pageSettings: { pageSize: 6, pageCount: 5 }, 
      columns: [ 
        { 
          field: 'OrderID', headerText: 'Order ID', isPrimaryKey: true, isVisible: false, textAlign: 'Right', width: 120 }, 
        { field: 'ShipName', headerText: 'Ship Name',foreignKeyField:"EmployeeID",foreignKeyValue:"EmployeeID",dataSource:@Html.Raw(Json.Serialize(@ViewData["dataSource"])), width: 120, editType: 'dropdownedit' }, 
        { field: 'ShipCountry', headerText: 'Ship Country',foreignKeyField:"CustomerID",foreignKeyValue:"CustomerID",dataSource:@Html.Raw(Json.Serialize(@ViewData["dataSource"])), width: 120 }, 
      ], 
    } 
    } 
</script> 


The value after the CRUD actions in the grid is sent to the server side using the parameters that we have already defined in the model class. You can get the added, modified, deleted records based on the parameters. Please refer the below code example and screenshot for your reference, 

 

Screenshot : 
 

Regards 
Vignesh Sivagnanam 



AH Ahmed March 30, 2021 10:25 AM UTC

Thanks, For your fast response,
Your code run without any errors, then update my code to your code.
but,
Unfortunately, The same error 500 in  child grid ,why the return child grid data not a list of my model although the parent grid view return correct date format with correct model.
My updated Code:
In Controller:-
public IActionResult Index()
        {
           
                var Departmnets = GetAllDepartmentDTOs().ToList();
                ViewBag.FromDepartmentList = Departmnets.ToList();
                ViewBag.TargetDepartmentList = Departmnets.ToList();

                var Positions = GetAllPositionDTOs().ToList();
                ViewBag.FromPositionList = Positions.ToList();
                ViewBag.TargetPositionList = Positions.ToList();

                ViewBag.DepartmentList = Departmnets.ToList();
                ViewBag.PositionList = Positions.ToList();
                ViewBag.TicketTypeList = GetAllTicketTypeDTOs().ToList();

                return View();
 
        }
       public IActionResult UrlDatasource([FromBody] DataManagerRequest dm)
        {
            IEnumerable Paths = AssembleAPIHandler.GetList<TicketPathReferenceDTO>("TicketPathReference/GetTicketPathReferencesOnlyNotDeleted").Result;
            var DataSource = _mapper.Map<IEnumerable<TicketPathReferenceViewModel>>(Paths);
            DataOperations operation = new DataOperations();
            if (dm.Search != null && dm.Search.Count > 0)
            {
                DataSource = operation.PerformSearching(DataSource, dm.Search);  //Search
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                DataSource = operation.PerformSorting(DataSource, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
            }
            int count = DataSource.Cast<TicketPathReferenceViewModel>().Count();
            if (dm.Skip != 0)
            {
                DataSource = operation.PerformSkip(DataSource, dm.Skip);         //Paging
            }
            if (dm.Take != 0)
            {
                DataSource = operation.PerformTake(DataSource, dm.Take);
            }
            return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
        }

        public IActionResult UrlDatasourceChild([FromBody] DataManagerRequest dm)
        {
            IEnumerable  Paths =  AssembleAPIHandler.GetList<TicketConfigurationPathDTO>("TicketConfigurationPath/GetTicketConfigurationPathsNotDeleted").Result;
            var DataSource = _mapper.Map<IEnumerable<TicketConfigurationPathViewModel>>(Paths);
            DataOperations operation = new DataOperations();
            if (dm.Search != null && dm.Search.Count > 0)
            {
                DataSource = operation.PerformSearching(DataSource, dm.Search);  //Search
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                DataSource = operation.PerformSorting(DataSource, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
            }
            int count = DataSource.ToList<TicketConfigurationPathViewModel>().Count();
            if (dm.Skip != 0)
            {
                DataSource = operation.PerformSkip(DataSource, dm.Skip);         //Paging
            }
            if (dm.Take != 0)
            {
                DataSource = operation.PerformTake(DataSource, dm.Take);
            }
            return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
        }
        public IActionResult BatchUpdate([FromBody] CRUDModel batchmodel)
        {
            IEnumerable<TicketPathReferenceDTO> Paths = AssembleAPIHandler.GetList<TicketPathReferenceDTO>("TicketPathReference/GetTicketPathReferencesOnlyNotDeleted").Result;
            var DataSource = _mapper.Map<IEnumerable<TicketPathReferenceViewModel>>(Paths);
            if (batchmodel.Changed != null)
            {
                for (var i = 0; i < batchmodel.Changed.Count(); i++)
                {
                   //Code
                }
            }

            if (batchmodel.Deleted != null)
            {
                for (var i = 0; i < batchmodel.Deleted.Count(); i++)
                {
                   //Code
                }
            }

            if (batchmodel.Added != null)
            {
                for (var i = 0; i < batchmodel.Added.Count(); i++)
                {
                    //Code
                }
            }
            var data = DataSource.ToList();
            return Json(data);

        }

        public IActionResult BatchUpdateChild([FromBody] CRUDModelChild batchmodel)
        {
             IEnumerable  Paths =  AssembleAPIHandler.GetList<TicketConfigurationPathDTO>("TicketConfigurationPath/GetTicketConfigurationPathsNotDeleted").Result;
            var childData = _mapper.Map<IEnumerable<TicketConfigurationPathViewModel>>(Paths);
           
            if (batchmodel.Changed != null)
            {
                for (var i = 0; i < batchmodel.Changed.Count(); i++)
                {
                    //Code
                }
            }

            if (batchmodel.Deleted != null)
            {
                for (var i = 0; i < batchmodel.Deleted.Count(); i++)
                {
                    //Code
                }
            }

            if (batchmodel.Added != null)
            {
                for (var i = 0; i < batchmodel.Added.Count(); i++)
                {
                    //Code
                }
            }
            var data = childData.ToList();
            return Json(data);

        }


In Index.cshtml
In Pictures below 
ChildGrid
ParentGrid




AH Ahmed March 30, 2021 10:26 AM UTC

Models 
//child
 public class TicketConfigurationPathViewModel 
    {
        public int ID { get; set; }
        public int? TicketPathReferenceID { get; set; }
       
        [Display(Name = "FromDepartment", ResourceType = typeof(ResourcesWeb.PayrollResource))]
        public int? DepartmentID { get; set; }

        [Display(Name = "TargetDepartment", ResourceType = typeof(ResourcesWeb.PayrollResource))]
        public int? TargetDepartmentID { get; set; }


        [Display(Name = "FromPosition", ResourceType = typeof(ResourcesWeb.PayrollResource))]
        public int? PositionID { get; set; }

        [Display(Name = "TargetPosition", ResourceType = typeof(ResourcesWeb.PayrollResource))]
        public int? TargetPositionID { get; set; }

    }
//parent 
 public class TicketPathReferenceViewModel 
    {
        public int TicketPathReferenceID { get; set; }

        
        [Required]
        [Display(Name = "TicketTypeName", ResourceType = typeof(ResourcesWeb.PayrollResource))]
        public int TicketTypeID { get; set; }

        [Required]
        [Display(Name = "TicketDepartment", ResourceType = typeof(ResourcesWeb.PayrollResource))]
        public int TicketDepartmentID { get; set; }

        [Required]
        [Display(Name = "Position", ResourceType = typeof(ResourcesWeb.PayrollResource))]
        public int PositionID { get; set; }
    }



PG Praveenkumar Gajendiran Syncfusion Team March 31, 2021 01:18 PM UTC

Hi Ahmed,

Based on your provided information, we would like to inform you that the HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request. We suspect that the error may be occurred if the provided model definition and model data  type in server will be conflicted. So please ensure this case at your end.

If you still face the same issue, we suggest you bind the ActionFailure event in Grid to catch the Exceptions and share the screenshot of the error details in that ActionFailure event. If possible share us a simple sample to replicate the problem which will be helpful for us to validate the reported issue at our end and provide solution as early as possible 


Regards,
Praveenkumar G 


Marked as answer

AH Ahmed March 31, 2021 02:47 PM UTC

Thanks a lot for Response ,
ActionFailure  is the best way to get the error in grid 
my code run without any errors 
Thanks again


PG Praveenkumar Gajendiran Syncfusion Team April 1, 2021 09:32 AM UTC

Hi Ahmed, 
Thanks for the update. We are glad to hear that your query is resolved.

Please get back to us if you need any further assistance. 

Regards, 
Praveenkumar G. 


Loader.
Up arrow icon