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

How to delete the item from my crud on my server using the destroy method

6 Answers 134 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lucas
Top achievements
Rank 1
Lucas asked on 07 Jul 2020, 06:45 PM
Hello, how are you?
I'm from Brazil, very pleased.

So, I want to press delete, delete also on my backend, not only on my client, I used the destroy method, but it only deletes on the client, it is not deleting from my DB, anyway, where am I wrong?

public IActionResult Delete(Guid? id, [DataSourceRequest] DataSourceRequest request)
        {
            try
            {
                var game = UnitOfWork.Game.Get(id);
                UnitOfWork.Game.Delete(game);
                UnitOfWork.SaveChanges();

                return Json(game, request);
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }

        }

And now my Grid:

@(Html.Kendo().Grid<GameListViewModel>()
      .Name("Grid")
      .Columns(columns =>
      {
          columns.Bound(p => p.Name).Title("Nome");
          columns.Bound(p => p.Price).Title("Preço");
          columns.Bound(p => p.Description).Title("Descrição");
          columns.Bound(p => p.GenderName).Title("Categoria");
          columns.Command(c => c.Edit().HtmlAttributes(new {title = "Editar Item"})).Title("Editar");
          columns.Command(read => read.Destroy().HtmlAttributes(new {title = "Deletar item"})).Title("Deletar");
      })
      .Pageable()
      .Sortable()
      .Scrollable()
      .Filterable()
      .HtmlAttributes(new { style = "height:430px;" })
      .DataSource(dataSource => dataSource
          .Ajax()
          .PageSize(20)
          .Read(read => read.Action("TelerikGrid", "Game"))
          .Update(read => read.Action("Edit", "Game", new {id = "{0}"}))
          .Destroy(read => read.Action("Delete", "Game"))
      ))

Probably my call is not going to the controller, how can I resolve it? Thank you.

6 Answers, 1 is accepted

Sort by
0
Accepted
Tsvetomir
Telerik team
answered on 10 Jul 2020, 02:28 PM

Hi Lucas,

With the current set up, I have noticed that you are trying to delete an item based on its ID. However, I have not seen the logic that is needed to send the Id. 

Using the built-in options of the Telerik UI Grid, accept the deleted data item and access its ID explicitly:

public IActionResult Delete(Guid? id, [DataSourceRequest] DataSourceRequest request, GameListViewModel model)
        {
            // access the ID of the "model" variable and pass it to the Get() method
           // .   .    .
        }

 

Regards,
Tsvetomir
Progress Telerik

0
Lucas
Top achievements
Rank 1
answered on 13 Jul 2020, 11:37 AM
I got it like this:

 public IActionResult Delete (Guid? id, [DataSourceRequest] DataSourceRequest request, GameListViewModel model)
        {
            try
            {
                var game = UnitOfWork.Game.Get (id);
                UnitOfWork.Game.Delete (game);
                UnitOfWork.SaveChanges ();

                return Json (game, request);

            }
            catch (Exception e)
            {
                throw new ArgumentException (e.Message);
            }
        }

Is correct?
What should I return for exclusion to work?
0
Lucas
Top achievements
Rank 1
answered on 13 Jul 2020, 12:14 PM
I managed to solve guys.
For those who will need help in the future, I passed a Model on my grid:

 .Model (model => model.Id (p => p.Id))

And I called the controller like this:

[AcceptVerbs ("Post")]
        public IActionResult Delete (Guid? id, [DataSourceRequest] DataSourceRequest request)
        {
            try
            {
                var game = UnitOfWork.Game.Get (id);
                UnitOfWork.Game.Delete (game);
                UnitOfWork.SaveChanges ();

                return Json (game, request);

            }
            catch (Exception e)
            {
                throw new ArgumentException (e.Message);
            }
        }
0
Tsvetomir
Telerik team
answered on 16 Jul 2020, 10:34 AM

Hi Lucas,

The Id for the data source of the grid is a crucial part of any of the create/update/delete operations. This is the field that is used to distinguish the items from one another. 

If there is anything that I can help with, let me know.

 

Kind regards,
Tsvetomir
Progress Telerik

0
Lucas
Top achievements
Rank 1
answered on 16 Jul 2020, 11:28 AM
Yes you can.
I couldn't edit the item, I tried everything but I couldn't.
Can you help me?

My Controller:

 [AcceptVerbs("Post")]
        public ActionResult Edit(GameEditViewModel model, [DataSourceRequest] DataSourceRequest request)
        {
            if (!ModelState.IsValid)
            {
                model.BindData(UnitOfWork);
                return View(model);
            }
            var game = UnitOfWork.Game.Get(model.Id);
            game.Name = model.Name;
            game.Description = model.Description;
            game.Price = model.Price;
            game.Gender = model.GenderId.HasValue ? UnitOfWork.Gender.Get(model.GenderId) : null;
            UnitOfWork.SaveChanges();
            //return Json(game, request);
            return Json(new[] { game }.ToDataSourceResult(request, ModelState));
        }

I can get him to ask my controller, but he only updates on the client.
I've already passed the Model ID but it doesn't work.

My Grid:


@(Html.Kendo().Grid<GameListViewModel>()
      .Name("Grid")
      .Columns(columns =>
      {
          columns.Bound(p => p.Name).Title("Nome");
          columns.Bound(p => p.Price).Title("Preço");
          columns.Bound(p => p.Description).Title("Descrição");
          columns.Bound(p => p.GenderName).Title("Categoria");
          columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172).Title("Opções");
      })
      .Pageable()
      .Sortable()
      .Editable(editable => editable.Mode(GridEditMode.PopUp))
      .Scrollable()
      .Filterable()
      .HtmlAttributes(new { style = "height:430px;" })
      .DataSource(dataSource => dataSource
          .Ajax()
          .Batch(true)
          .PageSize(20)
          .Model(model => {
              model.Id(p => p.Id);
                              model.Field(p => p.Id);
          })
          .Read(read => read.Action("TelerikGrid", "Game"))
          .Update(update => update.Action("Edit", "Game"))
          .Destroy(destroy => destroy.Action("Delete", "Game"))
      ))

Do I need ajax to edit?
Where can the error be?

Thank you.
0
Tsvetomir
Telerik team
answered on 21 Jul 2020, 09:54 AM

Hi Lucas,

I have investigated the provided code snippet and I have noticed that you have set the Batch option of the data source. It formats the request's parameters into a collection and it cannot be bound to a single object. 

Could you try removing it and see if that makes a difference?

 

Kind regards,
Tsvetomir
Progress Telerik

Tags
Grid
Asked by
Lucas
Top achievements
Rank 1
Answers by
Tsvetomir
Telerik team
Lucas
Top achievements
Rank 1
Share this question
or