Telerik Grid always returns 0 instead of a value when trying to update grid data

1 Answer 88 Views
Grid
Rajesh
Top achievements
Rank 1
Iron
Rajesh asked on 09 Aug 2023, 09:10 PM

Hello i am using Oracle Database and have a model  called CableView Model  that i pass to grid  and CableInfoID is my  intended  id for the grid. I have issues updating grid data as always it returning 0 instead of a value.What am i doing wrong?

 

grid code

@(
                            Html.Kendo().Grid<DI_IPMS_KENDO.Models.CableViewModel>()
                                .Name("CableNetGrid")
                                .Columns(columns =>
                                {
                                    columns.Bound(p => p.CableInfoID);
                                    columns.Bound(p => p.CableId).Title("Cable").Width(100);
                                    columns.Bound(p => p.SwitchName).Width(100);
                                    columns.Bound(p => p.Port).Width(100);
                                    columns.Bound(p => p.TechnicianAssigned).Title("TechnicianAssigned").Width(150);
                                    columns.Bound(p => p.AvailableIp).Width(100);
                                    columns.Bound(p => p.SubnetMask).Width(100);
                                    columns.Bound(p => p.Comments).Width(100);
                                    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
                                })
                                .ToolBar(toolbar => toolbar.Create())
                                .Editable(editable => editable.Mode(GridEditMode.PopUp))
                                .Pageable()
                                .Sortable()
                                .Scrollable()
                                .Events(ev => ev.Edit("onEdit"))
                                .Events(ev => ev.BeforeEdit("onEdit"))
                                .Events(ev => ev.Cancel("onCancel"))
                                .HtmlAttributes(new { style = "height:430px;" })
                                .DataSource(dataSource => dataSource
                                .Ajax()
                                .PageSize(20)
                                .Events(events => events.Error("error_handler"))
                                .Model(model =>
                                {
                                    model.Id(p => p.CableInfoID);
                                    model.Field(id => id.CableInfoID).DefaultValue(-1);
                                })
                            .Create(update => update.Action("Cables_Create", "Home").Data("getData"))
                            .Read(read => read.Action("Cables_Read", "Home").Data("getID"))
                            .Update(update => update.Action("Cables_Update", "Home").Data("getData"))
                            .Destroy(update => update.Action("Cables_Destroy", "Home"))
                            )
                            )

 

 

 

 

       

 public class CableViewModel

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



        [Required]
        [DisplayName("Cable ID")]
        public string CableId { get; set; }


        [Required]
        [DisplayName("Switch Name")]
        public string SwitchName { get; set; }

        [Required]
        [DisplayName("Port")]
        public string Port { get; set; }

        [ScaffoldColumn(false)]
        public string ToolID { get; set; }
        //public int ?ToolRecID { get; set; }
        [ScaffoldColumn(false)]
        public int ToolRecID { get; set; }

        [UIHint("_AdminEmp")]
        [Required]
        [DisplayName("Technician Assigned")]
        public string TechnicianAssigned { get; set; }

        [DisplayName("Available IP")]
        public string AvailableIp { get; set; }

        [DisplayName("Subnet Mask")]
        public string SubnetMask { get; set; }

        [DisplayName("Gateway")]
        public string Gateway { get; set; }
        public string Comments { get; set; }
    }

 

 

i have  update method as follows ..please ignore other fields as i am returning from view or grid..

Everytime i try to update  the grid row the CableInfoID is 0 

  public ActionResult Cables_Update([DataSourceRequest] DataSourceRequest request, CableViewModel product, int ToolRecID, string toolID, string Empltext)
        {
            if (ModelState.IsValid && product != null)
            {

                // Create a new Product entity and set its properties from the posted ProductViewModel.
                var entity = new DiIpmsCableTeamCableInfo
                {
                    RecordId = product.CableInfoID,
                    CableId = product.CableId,
                    SwitchName = product.SwitchName,
                    Port = product.Port,
                    TechnicianAssigned = Empltext,
                    ToolId = product.ToolID,
                    ToolRecId = ToolRecID,
                    AvailableIp=product.AvailableIp,
                    SubnetMask=product.SubnetMask
                };

                // Attach the entity.
                _db.DiIpmsCableTeamCableInfos.Attach(entity);
                // Change its state to Modified so Entity Framework can update the existing product instead of creating a new one.
                _db.Entry(entity).State = EntityState.Modified;
                // Or use ObjectStateManager if using a previous version of Entity Framework.
                // northwind.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
                // Update the entity in the database.
                _db.SaveChanges();

            }

            // Return the updated product. Also return any validation errors.
            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }

 

In the grid  update method 

1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander
Telerik team
answered on 14 Aug 2023, 02:59 PM

Hi Rajesh,

I would initially like to say a big thank you for adding all the relevant code portions that are currently incorporated on your premises.

I have carefully gone over Grid's component and Model declaration and I managed to conclude that the behavior is due to the inclusion of the [ScaffoldColumn] DataAnnotation. Internally, this will inevitably cause the "CableInfoID" to be prohibited from participating in the editing operations for the Grid.

Thus, I would recommend either omitting the aforementioned attribute. Or, if this option is not feasible provide a "DTO" model that will be used in the Grid model instead.

Alternatively, the Grid component exposes the ability to explicitly induct a custom PopUp template that may hold any arbitrary markup as per your requirements. In the context of your scenario, you can then induct an editor for the CableInfoID field there.

We have a dedicated GitHub example that illustrates such a scenario for the Telerik UI for ASP.NET Core Grid that can be found here:

https://github.com/telerik/ui-for-aspnet-core-examples

Here is the full configuration for the custom popup example in particular:

  • View
  • Popup Editor - should be allocated within the "~Views\Shared\EditorTemplates" folder as the Grid will look for the template within this folder location.
  • Model
  • Controller

Please give these suggestions a try and let me know how it works out for you.

Kind Regards,
Alexander
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Rajesh
Top achievements
Rank 1
Iron
commented on 15 Aug 2023, 03:51 PM

Thank You for the details and  running through git hub project with various examples.Thats helps a lot.
Tags
Grid
Asked by
Rajesh
Top achievements
Rank 1
Iron
Answers by
Alexander
Telerik team
Share this question
or