CRUD Create Update Delete from database. (I have seen the demo but I need a little help)

1 Answer 127 Views
Grid
Andi
Top achievements
Rank 1
Iron
Iron
Andi asked on 05 Oct 2022, 04:45 PM | edited on 05 Oct 2022, 04:47 PM

Hello this is my Controller in my code I am trying to update or delete from the database.

I get a 200 status which says its succesful but it never gets updated or deleted.

I can read and create fine but updating is a little challenging.

this is the response i get from the debbuger when i edit a row:

Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished HTTP/1.1 POST https://localhost:57770/Grid/ClientsEdit/14416 application/x-www-form-urlencoded;+charset=UTF-8 176 - 200 - application/json;+charset=utf-8 683.0242ms
public ActionResult ClientsEdit(int id, ClientDatum clientDatum, [DataSourceRequest] DataSourceRequest request)
        {
            PrincipalContext pc = new PrincipalContext(ContextType.Domain, "WIX");
            UserPrincipal up = UserPrincipal.FindByIdentity(pc, User.Identity.Name);
            GroupPrincipal ad = GroupPrincipal.FindByIdentity(pc, "Administrators");
            GroupPrincipal gr = GroupPrincipal.FindByIdentity(pc, "Gr");
            clientDatum = _clientContext.ClientData.Find(id);
            var clients = _clientContext.ClientData.ToList();
            var dsClient = cleints.ToDataSourceResult(request);
         
            if (id == null)
            {
                return BadRequest();
            }
            else if (gangDatum == null)
            {
                return NotFound();
            }
            else if (User.Identity.IsAuthenticated && (up.IsMemberOf(ad) || up.IsMemberOf(gr)))
            {
                if (ModelState.IsValid)
                { 
                    _clientContext.ClientData.Update(clientDatum);
                    _clientContext.Entry(clientDatum).State = EntityState.Modified;
                    _clientContext.SaveChanges();
                }
                return Json(clients);
            }
            else
            {
                string msg = "You need to authenticate to create a user";
                return Json(msg);
            }
        }



<div class="row">
    <div class="col-12">
        @(Html.Kendo().Grid <TelerikGangsApp.Models.ClientDatum>()
                            .Name("grid")
                            .Columns(columns =>
                            {   
                                //columns.Bound(p => p.Id);
                                columns.Bound(p => p.FirstName);
                                columns.Bound(p => p.MiddleName);
                                columns.Command(command => command.Edit()).Width(150);
                                columns.Command(command => command.Destroy()).Width(150);
                            })
                            .ToolBar(toolbar => 
                            {
                                toolbar.Create();
                                //toolbar.Save();
                                toolbar.Pdf();
                                toolbar.Search();
                            })
                            .Pdf(pdf => pdf
                            //.AllPages()
                            .AvoidLinks()
                            .PaperSize("A1")
                            .Margin("2cm", "1cm", "1cm", "1cm")
                            .Landscape()
                            .RepeatHeaders()
                            .TemplateId("page-template")
                            .FileName("Gangs.pdf")
                            .ProxyURL(Url.Action("PdfExport", "Grid"))
                            )
                            .Editable(editable => editable.Mode(GridEditMode.InLine))
                            .Pageable()
                            .Sortable()
                            .Scrollable()
                            .Filterable()
                            .Groupable()
                            .HtmlAttributes(new { style = "height:550px;" })
                            .DataSource(dataSource => dataSource
                                .WebApi()
                                .Batch(false)
                                .PageSize(20)
                                .ServerOperation(false)
                                .Model(model => {
                                    model.Id(client => client.Id);
                                    model.Field(client => client.Id).Editable(false);
                                })
                                .Read(read => read.Action("ClientRead", "Grid").Type(HttpVerbs.Get))
                                .Create(create => create.Action("ClientCreate", "Grid").Type(HttpVerbs.Post))
                                .Update(update => update.Action("ClientsEdit", "Grid", new {id = "{0}"}).Type(HttpVerbs.Post))
                                .Destroy(destroy => destroy.Action("ClientDelete", "Grid", new {id = "{0}"}).Type(HttpVerbs.Post))
                            )
        )
    </div>
</div>

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 10 Oct 2022, 12:20 PM

Hello Andi,

Thank you for the shared code snippets.

To display the updated Grid data when a specified record is edited, it is required to return the respective record back to the client. 

For example, when the Grid is bound in Web API, the Update operation returns the DataSourceResult object:

public ActionResult ClientsEdit(int id, ClientDatum clientDatum, [DataSourceRequest] DataSourceRequest request)
{
            ....    
            if (id == null)
            {
                return BadRequest();
            }
            else if (gangDatum == null)
            {
                return NotFound();
            }
            else if (User.Identity.IsAuthenticated && (up.IsMemberOf(ad) || up.IsMemberOf(gr)))
            {
                if (ModelState.IsValid)
                { 
                    _clientContext.ClientData.Update(clientDatum);
                    _clientContext.Entry(clientDatum).State = EntityState.Modified;
                    _clientContext.SaveChanges();
                }
                return new ObjectResult(new DataSourceResult { Data = new[] { clientDatum }, Total = 1 });
            }
            else
            {
                string msg = "You need to authenticate to create a user";
                return Json(msg);
            }
}

In addition, I am attaching a runnable sample for your convenience.

Hopefully, it will help you to resolve the issue.

 

 

Regards, Mihaela Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Andi
Top achievements
Rank 1
Iron
Iron
commented on 02 Nov 2022, 05:16 PM

Thank you Mihaela. 
Tags
Grid
Asked by
Andi
Top achievements
Rank 1
Iron
Iron
Answers by
Mihaela
Telerik team
Share this question
or