Wrong methods fired

4 posts, 0 answers
  1. Leonardo
    Leonardo avatar
    3 posts
    Member since:
    Aug 2017

    Posted 08 Aug Link to this post

    Im having trouble with the KendoUI grid for .NETCore, always firing the create method.

    Im using the edit inline template, expecting to fire the update method in the controller when the user updates a row.

    Also the delete method is not being fired.

    Basically made a helper class, put all the properties from 3 different models in a single class so i can handle it in the current form.

    I think its something about not recognizing if its a new item on the grid, or a old item.

    Any help would be greatly appreciated

      Helper class example(didnt add the whole class, bunch of int fields and strings):

     public class DJForm
        {
            
            public int Id { get; set; }

            
            [ScaffoldColumn(false)]
            public int? UserId { get; set; }

        
            [ScaffoldColumn(false)]
            public int LocationId { get; set; }

            [ScaffoldColumn(false)]
            public DateTime? ExposureMonth { get; set; }

            [ScaffoldColumn(false)]
            public string FiltrationUser { get; set; }

            [ScaffoldColumn(false)]
            public string PostWeighingUser { get; set; }

     
       }

    Grid:

                      @(Html.Kendo().Grid<RTEMSDataLayer.UIResources.DJForm>()
                                .Name("grid")
                                .Columns(columns =>
                                {
                                    columns.Bound(p => p.LocationJarId).Width(70).HeaderHtmlAttributes(new { style = "font color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.ExposureStartOn).Width(170).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.ExposureEndOn).Width(170).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;width:170px;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.FilterId).Width(95).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.CrucibleNumber).Width(70).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.LiquidSample).Width(70).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.PreWeight).Width(70).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.PostWeight).Width(70).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Bound(p => p.RowComment).Width(200).HeaderHtmlAttributes(new { style = "font-color:black;font-size:x-small;height:auto;white-space:normal;text-align: center;font-weight:bold" });
                                    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(100);
                                })
                                .ToolBar(toolbar => toolbar.Create())
                                .Editable(editable => editable.Mode(GridEditMode.InLine))
                                .Pageable()
                                .Sortable()
                                .Scrollable()
                                .HtmlAttributes(new { style = "height:430px;" })
                                .DataSource(dataSource => dataSource
                                    .Ajax()
                                    .PageSize(30)
                                    .Events(e => e.Error("error_handler"))
                                .Model(model =>
                                {
                                    model.Id(p => p.Id);                             
                                })
                                    .Create(update => update.Action("EditingInline_Create", "DustJarDatas").Data("additionalData"))
                                    .Read(read => read.Action("EditingInline_Read", "DustJarDatas"))
                                    .Update(update => update.Action("EditingInline_Update", "DustJarDatas"))
                                    .Destroy(update => update.Action("EditingInline_Destroy", "DustJarDatas"))
                                )
                        )

    Update Action:

            [AcceptVerbs("Post")]
            public IActionResult EditingInline_Update([DataSourceRequest] DataSourceRequest request, DJForm form)
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("error", "error");
                    return Json(new[] { form }.ToDataSourceResult(request, ModelState));
                }

                if (form != null && ModelState.IsValid)
                {
                    //DustJarData dj = _context.DustJarData.Find(form.LocationJarId);

                    //dustjardata.LocationJarId = form.LocationJarId;
                    //dustjardata.ExposureStartOn = dateExpStart;
                    //dustjardata.ExposureEndOn = dateExpEnd;
                    //   productService.Update(p);
                }

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

    Create Action example:

    public IActionResult EditingInline_Create(string data, [DataSourceRequest] DataSourceRequest request, DJForm form)
            {
                string[] rawData = data.Split(',');
                if (rawData[0] != null) {
                    form.UserId = Int32.Parse(rawData[0]);
                }
                if (rawData[1] != null) {
                    form.LocationId = Int32.Parse(rawData[1]);
                }
                if (rawData[2] != null) {
                    form.ExposureMonth = DateTime.Parse(rawData[2]);
                }
                if (rawData[3] != null) {
                    form.FiltrationUser = rawData[3];
                }
                if (rawData[4] != null) {
                    form.PostWeighingUser = rawData[4];
                }
                if (rawData[5] != null) {
                    form.ProcessSessionOn = DateTime.Parse(rawData[5]);
                }
                if (rawData[6] != null) {
                    form.WeighingSessionOn = DateTime.Parse(rawData[6]);
                }
                if (rawData[7] != null) {
                    form.ProcessTemp = Int32.Parse(rawData[7]);
                }
                if (rawData[8] != null)
                {
                    form.ProcessRh = Int32.Parse(rawData[8]);
                }
                if (rawData[9] != null)
                {
                    form.WeighingTemp = Int32.Parse(rawData[9]);
                }
                if (rawData[10] != null)
                {
                    form.WeighingRh = Int32.Parse(rawData[10]);
                }

                if (!ModelState.IsValid) {
                    ModelState.AddModelError("101", "errors");
                    ViewBag.Error = "102";
                    return Json(new[] { form }.ToDataSourceResult(request, ModelState));
                }

                if (form != null && ModelState.IsValid)
                {

    ....

    ....

    ....

    }

  2. Tsvetina
    Admin
    Tsvetina avatar
    2085 posts

    Posted 11 Aug Link to this post

    Hi Leonardo,

    When an item is saved in the DataSource, there is an isNew() call which checks if the current item ID equals the default value of the field.
    isNew: function() {
        return this.id === this._defaultId;
    }
    If the item is recognized as new, a Create operation is fired for it.

    For example, when the model Id is a numeric field, its default value is 0. If you create an item with Id=0 and then try to edit it, upon save, it will trigger a Create operation because its Id equals the default value for a non-nullable numeric field. 

    Check if this is the cause of the undesired behavior on your side and if it is, ensure that you start your Ids count from 1.

    Regards,
    Tsvetina
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Leonardo
    Leonardo avatar
    3 posts
    Member since:
    Aug 2017

    Posted 14 Aug in reply to Tsvetina Link to this post

    Thanks for the reply, after making a few changes i managed to get it working, changing the model id to:

    model.Id<int>(p => p.LocationJarId);  

    And it works great creating and updating a row in the grid.

     

    But when i try to Cancel the changes when editing, and Deleting an specific record this error shows up:

    kendo.all.min.js:27 Uncaught TypeError: Cannot read property 'uid' of undefined
        at kendo.all.min.js:27
        at z (kendo.all.min.js:27)
        at R (kendo.all.min.js:27)
        at kendo.all.min.js:27
        at init._eachItem (kendo.all.min.js:27)
        at init._eachPristineItem (kendo.all.min.js:27)
        at init._executeOnPristineForModel (kendo.all.min.js:27)
        at init._removePristineForModel (kendo.all.min.js:27)
        at init._accept (kendo.all.min.js:27)
        at Object.<anonymous> (kendo.all.min.js:27)

    Im still using the helper class, to contain the fields that i want to display in the kendo grid.

  4. Tsvetina
    Admin
    Tsvetina avatar
    2085 posts

    Posted 17 Aug Link to this post

    Hi Leonardo,

    Can you confirm that the LocationJarId field has only unique, non-default values? By non-default, I mean that it does not have 0 values for Int or empty string values for String type.
    The model Id has to be a field with unique, non-nullable and non-default values.

    Regards,
    Tsvetina
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top