This is a migrated thread and some comments may be shown as answers.

Grid will not post - Batch Editing

2 Answers 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 22 Feb 2018, 09:29 PM

What am I doing wrong.  I have a grid that refuses to post back to the proper action in the controller.  It continually goes back to the (get) Index action even though in the grid I am calling an action called Cleanup_Update.  When initially calling the page Cleanup_Read works fine.  Only when editing the inside of the grid and hitting save changes is it not going where it should go. 

Thanks in advance

 

View

@{
    ViewData["Title"] = "Index";
}
@Html.AntiForgeryToken()
 
<h2>Index</h2>
 
@(Html.Kendo().Grid<FacilityDataManagerMVC.Models.ViewModels.ErrorCleanup>
                ()
                .Name("grid")
                .Columns(columns =>
                {
                    columns.Bound(c => c.ID).Width(150);//.Hidden(true);
        columns.Bound(c => c.stationAssetID).Width(140).Filterable(ftb => ftb.Multi(true).CheckAll(true));
                    columns.Bound(c => c.stationID).Width(140).Filterable(ftb => ftb.Multi(true).CheckAll(true));
                    columns.Bound(c => c.errorType).Width(110).Filterable(ftb => ftb.Multi(true).CheckAll(true));
                    columns.Bound(c => c.readingDate).Width(110).Format("{0:MM/dd/yyyy}").Filterable(ftb => ftb.Multi(true).CheckAll(true));
        //columns.Bound(c => c.errorCorrected).Width(25);
        //change the column above to a checkbox
        columns.Bound(c => c.errorCorrected).ClientTemplate("<input type='checkbox' disabled='true' value='#= errorCorrected #' " + " # if (errorCorrected) { #" + "checked='checked'" + "# } #" + "/>");
                    columns.Bound(c => c.operatingHours).Width(25);
                    columns.Bound(c => c.totalDowntimeHours).Width(25);
                    columns.Bound(c => c.horsepowerHours).Width(25);
                    columns.Bound(c => c.fuelConsumed).Width(25);
                    columns.Bound(c => c.engineStartAttempts).Width(25);
                    columns.Bound(c => c.engineStartFailed).Width(25);
                    columns.Bound(c => c.engineStartSuccess).Width(25);
                    columns.Bound(c => c.startupDuration).Width(25);
                    columns.Bound(c => c.throughputMMCF).Width(25);
                    columns.Bound(c => c.averageUnitRPM).Width(25);
                    columns.Bound(c => c.oilUsageCompressor).Width(25);
                    columns.Bound(c => c.oilUsageEngine).Width(25);
                    columns.Bound(c => c.oilUsageManual).Width(25);
                })
                .ToolBar(toolbar =>
                {
                    toolbar.Save();
                })
                .Editable(editable => editable.Mode(GridEditMode.InCell))
                .Sortable()
                .Filterable()
                .Pageable()
                .Navigatable()
                .DataSource(dataSource => dataSource
                .Ajax()
                .Batch(true)
                .PageSize(20)
                .ServerOperation(false)
                .Events(events => events.Error("error_handler"))
                //set the fields to non-editable here
                .Model(model =>
                {
                    model.Field(p => p.ID).Editable(false);
                    model.Field(p => p.stationID).Editable(false);
                    model.Field(p => p.readingDate).Editable(false);
                    model.Field(p => p.errorType).Editable(false);
                    model.Field(p => p.stationAssetID).Editable(false);
                    model.Field(p => p.errorCorrected).Editable(false);
                })
                .Read(read => read.Action("Cleanup_Read", "Cleanup").Data("sendAntiForgery"))
                .Update(update => update.Action("Cleanup_Update", "Cleanup").Data("sendAntiForgery"))
                )
)
<script type="text/javascript">
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>
<script type="text/javascript">
    function sendAntiForgery() {
        return { "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val() }
    }
</script>
<style>
    .k-readonly {
        color: gray;
    }
</style>

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using FacilityDataManagerMVC.Data;
using FacilityDataManagerMVC.Models.ViewModels;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
 
namespace FacilityDataManagerMVC.Controllers
{
 
    public class CleanupController : Controller
    {
        private readonly FacilityDataManagerMVCContext _context;
 
        public CleanupController(FacilityDataManagerMVCContext context)
        {
            _context = context;
        }
        // GET: Cleanup
        public ActionResult Index()
        {
            return View();
        }
 
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult IndexPost([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ErrorCleanup> errors)
        {
 
            if (errors != null && ModelState.IsValid)
            {
                foreach (var product in errors)
                {
                    //productService.Update(product);
                    string x = "mystring";
                }
            }
 
            return Json(errors.ToDataSourceResult(request, ModelState));
        }
 
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Cleanup_Update([DataSourceRequest] DataSourceRequest request, [Bind]IEnumerable<ErrorCleanup> errors)
        {
            if (errors != null && ModelState.IsValid)
            {
                foreach (var product in errors)
                {
                    //productService.Update(product);
                    string x = "mystring";
                }
            }
 
            return Json(errors.ToDataSourceResult(request, ModelState));
        }
       
        public IActionResult Cleanup_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(getScadaErrors().ToDataSourceResult(request));
        }
        private IEnumerable<ErrorCleanup> getScadaErrors()
        {
            using (_context)
            {
                return _context.ScadaErrors.Select(scadaError => new ErrorCleanup
                {
                    ID = scadaError.Id,
                    stationAssetID = scadaError.StationAssetId,
                    stationID = scadaError.StationId,
                    errorType = scadaError.ErrorType,
                    readingDate = scadaError.ReadingDate,
                    operatingHours = scadaError.OperatingHrs,
                    totalDowntimeHours = scadaError.TtlDowntimeHrs,
                    horsepowerHours = scadaError.HrsepwrHrs,
                    fuelConsumed = scadaError.FuelConsumed,
                    engineStartAttempts = scadaError.EngineStartsAttempts,
                    engineStartFailed = scadaError.EngineStartsFailed,
                    engineStartSuccess = scadaError.EngineStartsSuccess,
                    startupDuration = scadaError.StartupDuration,
                    throughputMMCF = scadaError.ThruputMmcf,
                    averageUnitRPM = scadaError.AvgUnitRpm,
                    oilUsageCompressor = scadaError.OilToCompCyl,
                    oilUsageManual = scadaError.OilNewCcHand,
                    errorCorrected = scadaError.ErrorCorrected
                    //blowdownCount = (int)scadaError.BlowdownCnt
                }
                ).ToList();
            }
        }
    }
}

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 27 Feb 2018, 12:59 PM
Hi Michael,

Thank you for the provided code. I have investigated it and noticed that the Id of the model is not specified. It is mandatory to specify which field of the model is the Id since the grid tracks the state of each data item by its Id.

e.g.
.Model(model =>
                {
                    model.Id(p => p.ID);
                    model.Field(p => p.ID).Editable(false);
                    model.Field(p => p.stationID).Editable(false);
                    model.Field(p => p.readingDate).Editable(false);
                    model.Field(p => p.errorType).Editable(false);
                    model.Field(p => p.stationAssetID).Editable(false);
                    model.Field(p => p.errorCorrected).Editable(false);
                })

Could you please modify the configuration of the Model as illustrated in the above code block and let me know if the updated records are sent to the server as expected?

I look forward to your reply.


Regards,
Georgi
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Michael
Top achievements
Rank 1
answered on 27 Feb 2018, 03:31 PM
That did it.. you guys are awesome.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Michael
Top achievements
Rank 1
Share this question
or