I have a grid which is a part of bigger form. I want to be able to do all sorts of data manipulation without saving it immediately. When saving form, I need to save data in a grid.
I'm experiencing a problem when editing a row in InLine mode. When I click on "edit", then "cancel", the row disappears from the grid depending on how I load data into the grid.
If Read method looks like this, than canceling edit works good but I'm not able to save data temporarily.
public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request)
{
ToDestinations = _entities.ToDestinations.ToList();
//var data = HttpContext.Session.Get("ToDestinationsData");
//if (data != null)
//{
// ToDestinations = JsonConvert.DeserializeObject<List<ToDestination>>(Encoding.UTF8.GetString(data));
//}
return new JsonResult(ToDestinations.ToDataSourceResult(request));
}
On the other hand, if Read method looks like below, every row that I'm doing "edit" => "cancel" disappears. (This was a start to making grid data persistent while doing all kinds of data manipulation in a grid.)
public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request)
{
//ToDestinations = _entities.ToDestinations.ToList();
var data = HttpContext.Session.Get("ToDestinationsData");
if (data != null)
{
ToDestinations = JsonConvert.DeserializeObject<List<ToDestination>>(Encoding.UTF8.GetString(data));
}
return new JsonResult(ToDestinations.ToDataSourceResult(request));
}
This is my grid.
@(Html.Kendo().Grid<ToDestination>()
.Name("grid")
.Columns(columns =>
{
columns.ForeignKey(c => c.TravelTypeId, ds => ds.Read(read => read.Url(Url.Page("Create", "TravelTypes"))), "TravelTypeId", "TravelTypeName").Title("Vrsta putovanja");
columns.ForeignKey(c => c.CountryId, ds => ds.Read(read => read.Url(Url.Page("Create", "Countries"))), "CountryId", "CountryName", true).Title("Država")
.EditorTemplateName("CountryEditor"); // Specify a custom editor template for the Country dropdown.
columns.ForeignKey(c => c.PlaceId, ds => ds.Read(read => read.Url(Url.Page("Create", "Places"))), "PlaceId", "PlaceName", true).Title("Mjesto")
.EditorTemplateName("PlaceEditor"); // Specify a custom editor template for the Place dropdown.
columns.ForeignKey(c => c.CenterId, ds => ds.Read(read => read.Url(Url.Page("Create", "Centers"))), "CenterId", "CenterName", true).Title("Centar")
.EditorTemplateName("CenterEditor"); // Specify a custom editor template for the Center dropdown.
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.DataSource(ds => ds.Ajax()
.Create(c => c.Url("/TravelOrders/Create?handler=Create").Data("forgeryToken"))
.Read(r => r.Url("/TravelOrders/Create?handler=Read").Data("forgeryToken"))
.Update(u => u.Url("/TravelOrders/Create?handler=Update").Data("forgeryToken"))
.Destroy(d => d.Url("/TravelOrders/Create?handler=Destroy").Data("forgeryToken"))
.Model(m =>
{
m.Id(id => id.ToDestinationId);
})
))
I hope what I wrote makes sense.
What is the best practice to achieve saving grid data temporarily without "edit" => "cancel" bug appearing?