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

7 posts, 1 answers
  1. Lucas
    Lucas avatar
    4 posts
    Member since:
    Jul 2020

    Posted 07 Jul 2020 Link to this post

    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.
  2. Answer
    Tsvetomir
    Admin
    Tsvetomir avatar
    790 posts

    Posted 10 Jul 2020 Link to this post

    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

  3. Lucas
    Lucas avatar
    4 posts
    Member since:
    Jul 2020

    Posted 13 Jul 2020 in reply to Tsvetomir Link to this post

    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?
  4. Lucas
    Lucas avatar
    4 posts
    Member since:
    Jul 2020

    Posted 13 Jul 2020 Link to this post

    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);
                }
            }
  5. Tsvetomir
    Admin
    Tsvetomir avatar
    790 posts

    Posted 16 Jul 2020 Link to this post

    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

  6. Lucas
    Lucas avatar
    4 posts
    Member since:
    Jul 2020

    Posted 16 Jul 2020 in reply to Tsvetomir Link to this post

    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.
  7. Tsvetomir
    Admin
    Tsvetomir avatar
    790 posts

    Posted 21 Jul 2020 Link to this post

    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

Back to Top