Delete User from Table AspNetUsers MVC5

0

As I can delete users that I created in the AspNetUsers table, I work under MVC5.

My code is as follows,

AccountController.cs:

 //INDEX LIST USERS ADM
        public ActionResult Index()
        {
            userContext = new ApplicationDbContext();
            List<AspNetUserstb> users = (from o in userContext.Users
                                         join p in userContext.Roles on o.Roles.Select(t => t.RoleId).FirstOrDefault() equals p.Id
                                         select new AspNetUserstb
                                         {
                                             Id = o.Id,
                                             Email = o.Email,
                                             Rol = p.Name
                                         }).ToList();

            return View(users);
        }

My sight.

Index.cshtml:

<h2>Usuarios SI</h2>

<p>
    <button onclick="location.href='@Url.Action("Register")'" type="button" class="btn btn-success">Nuevo Usuario</button>
</p>
<div class="stylepagination">
    <table id="usersadm" class="table nowrap table-bordered table-hover display">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Email)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Rol)
                </th>
                <th><span class="glyphicon glyphicon-cog"></span> Administrar</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Email)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Rol)
                    </td>
                    <td style="text-align: center;">
                        <div class="btn-group btn-group-sm">
                            <button onclick="location.href='@Url.Action("Resetpw", "Account", new { id = item.Id })'" type="button" class="btn btn-default">Reset Password</button>
                            <button onclick="location.href='@Url.Action("Deluser", "Account", new { id = item.Id })'" type="button" class="btn btn-default">Eliminar</button>
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>

and finally my model.

AccountViewModels.cs:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SI_OldMutual.Models
{
    public class ExternalLoginConfirmationViewModel
    {
        [Required]
        [Display(Name = "Correo electrónico")]
        public string Email { get; set; }
    }

    public class ExternalLoginListViewModel
    {
        public string ReturnUrl { get; set; }
    }

    public class SendCodeViewModel
    {
        public string SelectedProvider { get; set; }
        public ICollection<System.Web.Mvc.SelectListItem> Providers { get; set; }
        public string ReturnUrl { get; set; }
        public bool RememberMe { get; set; }
    }

    public class VerifyCodeViewModel
    {
        [Required]
        public string Provider { get; set; }

        [Required]
        [Display(Name = "Código")]
        public string Code { get; set; }
        public string ReturnUrl { get; set; }

        [Display(Name = "¿Recordar este explorador?")]
        public bool RememberBrowser { get; set; }

        public bool RememberMe { get; set; }
    }

    public class ForgotViewModel
    {
        [Required]
        [Display(Name = "Correo electrónico")]
        public string Email { get; set; }
    }

    public class LoginViewModel
    {
        [Required]
        [Display(Name = "Correo electrónico")]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Contraseña")]
        public string Password { get; set; }

        [Display(Name = "¿Recordar cuenta?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Correo electrónico")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "El número de caracteres de {0} debe ser al menos {2}.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Contraseña")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirmar contraseña")]
        [Compare("Password", ErrorMessage = "La contraseña y la contraseña de confirmación no coinciden.")]
        public string ConfirmPassword { get; set; }

        [NotMapped]
        [Required]
        public string rol { get; set; }

    }

    public class ResetPasswordViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Correo electrónico")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "El número de caracteres de {0} debe ser al menos {2}.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Contraseña")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirmar contraseña")]
        [Compare("Password", ErrorMessage = "La contraseña y la contraseña de confirmación no coinciden.")]
        public string ConfirmPassword { get; set; }

        public string Code { get; set; }
    }

    public class ForgotPasswordViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Correo electrónico")]
        public string Email { get; set; }
    }

    public class AspNetUserstb
    {
        [Display(Name = "Id")]
        public virtual string Id { get; set; }

        [Display(Name = "Correo electrónico")]
        public virtual string Email { get; set; }

        [Display(Name = "Rol")]
        public virtual string Rol { get; set; }
    }
}

The only thing I need is to delete the user that I select in the table that I currently have, that table has the delete button and that button has the id of the user that I want to remove from the AspNetUsers table.

    
asked by ByGroxD 08.05.2017 в 21:48
source

1 answer

2

You must create the action to eliminate the user receiving the Id of the user that you want to delete, that Id currently you already send it in View :

<button onclick="location.href='@Url.Action("Deluser", "Account", new { id = item.Id })'" type="button" class="btn btn-default">Eliminar</button>

Controller Action:

public ActionResult Deluser(string id)
{
   using (var userContext = new ApplicationDbContext())
   {
        var objUser = (from p in userContext.Users
                                where p.Id == id
                                select p).FirstOrDefault();

        userContext.Users.Remove(objUser);

        userContext.SaveChanges();

        return RedirectToAction("Index","Account");   
   }
}

Basically, the operation is, look for the user, perform the delete and confirm the changes so that the database is updated, finally, only perform the redirect at Index so that the page is refreshed.

    
answered by 08.05.2017 / 22:19
source