Passing Data to Controller when Datasouce is a Model

How do I pass data to the controller for updating when my data source is coming from a view model? I have tried using the Url Adaptor but I don't need to get data from URL only updating it etc. I have provided the below code for further explanation.

Homecontroller.cs
public ActionResult ManageInventory()
        {
            var inventoryViewModel = new ManageInventoryViewModel
            {
                Inventory = _repository.GetInventoryItems()
            };
            return View(inventoryViewModel);

        }

ManageInventoryViewModel.cs
public class ManageInventoryViewModel
    {
        public List<Inventory> Inventory { get; set; } //this list has the properties I am actually using in the grid.
    }

ManageInventory.cshtml
@model InventoryManager.Web.Models.ManageInventoryViewModel

@Html.EJS().Grid("InventoryGrid").DataSource(Model.Inventory).Columns(col =>
{
    col.Field("InventoryID").HeaderText("Id").IsPrimaryKey(true).AllowEditing(false).Width("50").Add();
     etc.
}).AllowPaging(true).AllowSorting(true).AllowFiltering(true).PageSettings(page => page.PageSize(15)).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Dialog); }).Toolbar(new List<string>() { "Add", "Edit", "Update", "Cancel" }).Render()

3 Replies 1 reply marked as answer

SK Sujith Kumar Rajkumar Syncfusion Team January 18, 2021 11:48 AM UTC

Hi Danyelle, 
 
Greetings from Syncfusion support. 
 
Based on the query we could understand that your requirement is to update the data in the back-end using the controller method on performing the Grid CRUD actions alone. For this case, we suggest you to use the remote save adaptor for binding data to the Grid. With the remote save adaptor, you can directly set the model data as Grid’s data source and all the Grid actions will be performed in client-side except the CRUD actions. The CRUD actions can be mapped to server-side using the following properties - updateUrl, insertUrl, removeUrl, batchUrl, crudUrl. 
 
This is demonstrated in the below code snippet, 
 
@Html.EJS().DataSource(dataManager => { dataManager.Json(@Model.orderData.ToArray()).InsertUrl("/Home/Insert").RemoveUrl("/Home/Delete").UpdateUrl("/Home/Update").Adaptor("RemoteSaveAdaptor");}) 
 
So the corresponding controller action method defined for each CRUD action will be triggered with the action details. Using this you can update the data in your back-end and return back the response to the Grid in order to update it. 
 
We have prepared a sample based on this for your reference. You can download it from the following link, 
 
 
More details on the remote save adaptor can be found in the below help documentation site,    
    
 
Please get back to us if you require any further assistance. 
 
Regards, 
Sujith R 


Marked as answer

DA Danyelle January 18, 2021 09:14 PM UTC

This worked great! Thanks


TS Thiyagu Subramani Syncfusion Team January 19, 2021 03:55 AM UTC

Hi Danyelle , 

Thanks for your update. 

We are happy to hear that the provided solution works at your end. 

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

Regards, 
Thiyagu S 


Loader.
Up arrow icon