user1911
user1911

Reputation: 710

Delete row from grid and database with Kendo UI and MVC

I'm using a MVC project with Visual Studio 2012 and Kendo UI 2014.

I can delete a row from the grid and it disappears, but when I reload the page, the row that I deleted re-appears. I would like to remove from the database too.

This is my code:

.cshtml (View):

<div class="grid">
@(Html.Kendo().Grid<UsersModel>()
    .Name("grid")
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(r => r.Action("GetAccounts", "ManagerAccounts", new { area = "Admin" }))
        .Model(r => r.Id(p => p.Id))
        .Destroy("Delete", "ManagerAccounts")
    )
    .Columns(columns =>
    {
        columns.Bound(c => c.Id);
        columns.Bound(c => c.UserName);
        columns.Bound(c => c.Email);
        columns.Command(command => command.Destroy()).Width(120);
    })
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5)
    )
    .Scrollable()
    .Sortable()
    .Navigatable()
)
</div>

Controller:

public ActionResult Delete([DataSourceRequest] DataSourceRequest request, Everis.WebApps.Security.Models.ListadoUsuarioModel users)
{
    if (users != null && ModelState.IsValid)
    {
        AccountRepository.DeleteUsuarios(users.Id);
    }

    return Json(ModelState.ToDataSourceResult());
}

Repository (AccountRepository):

protected ApplicationDbContext Context = new ApplicationDbContext();

    public void DeleteUsuarios(string id)
    {
        var usuario = this.Context.Users.FirstOrDefault(x => x.Id == id);
        if (usuario != null)
        {
            this.Context.Users.Remove(usuario);                
        }
    }

IdentityModels (from ApplicationDbContext Context):

public class ApplicationUser : IdentityUser
{
    public bool Active { get; set; }
}

public class ApplicationRol : IdentityRole
{
    public Application Application { get; set; }
}

public class Application
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {            
    }

    public DbSet<Application> Applications { get; set; }

    public new DbSet<ApplicationRol> Roles { get; set; }

} 

Any idea to delete from database too?

Upvotes: 0

Views: 5413

Answers (1)

Harikant
Harikant

Reputation: 277

You can check this url.

this.Context.SaveChanges()

Upvotes: 2

Related Questions