0

I am using Inline editing in a Kendo Grid and I want to be able to get the current row data when doing an update.

Html.Kendo().Grid<WheresMyTool.Models.COMPANYADDRESS>()
        .Name("ShipToLocationsGrid")
        .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.InLine))
        .Pageable(pager => pager.PageSizes(new List<object> { 5, 10, 20, 100 }))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Model(model => model.Id(p => p.COMPANY))
            .Read(read => read.Action("IndexShipToLocations", "Company", new { company = Model.company  }))
            .Update(update => update.Action("ShipToLocations_Update"))
            .PageSize(20)
        ) 

Here is my update method

public ActionResult ShipToLocations_Update([DataSourceRequest] DataSourceRequest request, COMPANYADDRESS ca)
        {
            Repository repo = new Repository();
            repo.UpdateCompanyAddressRecord(ca, username);
            return Json(ca);
        }

I want to be able to access the current data. It seems like only the modified data is passed in.

Brian Kalski
  • 897
  • 9
  • 35
  • What do you mean by access the current data? Current from your view model, or current from your database model? Your COMPANYADDRESS should be a view model that is being passed to your Controller method ShipToLocations_Update(). This takes in your view model then updates the corresponding model in your data. – Kevin Aug 13 '19 at 21:27
  • I was hoping to get the data in the selected row from the grid. – Brian Kalski Aug 14 '19 at 01:23
  • I posted an edit in my answer to address your comment. – Kevin Aug 14 '19 at 19:24

1 Answers1

1
[HttpPost]
    public ActionResult EditTestStationInLine([DataSourceRequest] DataSourceRequest request, TestStationViewModel tsvm)
    {
        if(tsvm != null && ModelState.IsValid)
        {
            TestStation ts = _testStationService.Find(tsvm.Id);
            ts.ProductionLineId = tsvm.ProductionLineId;
            ts.ProdLine = _productionLineService.Find(tsvm.ProductionLineId);
            ts.Name = tsvm.Name;

            _testStationService.Update(ts);

            tsvm.Id = ts.Id;
            tsvm.ProductionLineId = ts.ProductionLineId;
        }

        return this.Json(new[] { tsvm }.ToDataSourceResult(request, ModelState));
    }

This is an example of one of my InLine edits. Your view model is used to create a database model. Once your database model is created, you can pass back the hidden attributes, in my case the Id and CreateAt. Hope this helps answer your question.

Edit:

function Edit(e) {
    var grid = $('#NAMEOFKENDOGRID').data('kendoGrid');
    var dataItem = grid.dataItem($(e.target).parents('tr'))
    var id = dataItem.id;
    $.ajax({//Makes an ajax call
        success: function (data) {
            $('#NAMEOFDIV').load("/Controller/Update/" + id);
        }
    });
}

You can make a custom command, conveniently called Edit, which then gets the Id from that row. It also makes an ajax call to your update method. Modify the ajax to fit your needs.

Kevin
  • 333
  • 1
  • 14
  • My issue is that the table I am working with doesn't have an identity column. It has two primary keys and they want to be able to edit one of those primary keys. If they edit one then you can't look up the original record. – Brian Kalski Aug 14 '19 at 01:25
  • @BrianKalski I mean...I don't think you should really be allowed to edit primary keys...That's kinda the point of them. – JamesS Aug 14 '19 at 09:56
  • I agree. This isn't my design. – Brian Kalski Aug 14 '19 at 15:25
  • Um, I guess if you want to update the primary key you can just have that as a column in your grid and update it that way. If you have any foreign keys that was mess a lot of stuff up but that is one way to go about it. – Kevin Aug 14 '19 at 19:23