Remove user without deleting from database

Asked

Viewed 104 times

2

I need help from you, I created two classes within my Controller, where I can remove a user from one page and send to another. As if deleting but not erasing from the database.

I created a variable in my database called Removed where I think it would be best to work, when Removed for = 1 it transfers this user to another page, which in this case would be as if I were deleting the user from that page

follows the code

Inside the Controller

public ActionResult ExcluirCadastro(int id = 0)
        {
            if (!Cookies.Exists("hid")) { 
                return RedirectToAction("Index", "Login", new { area = "Entrar" });
            }

            var hospitalId = int.Parse(Cookies.GetCookie("hid"));

            if (id <= 0 || hospitalId <= 0) {
                return RedirectToAction("Index");
            }

            var excluirCadastro = _cadastroService.GetByPatientId(id, hospitalId);

            if (excluirCadastro != null && excluirCadastro.HospitalId == hospitalId)
            {
                return View(excluirCadastro);
            }

            return RedirectToAction("Index");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeletarPacientes(int cadastroId)
        {
            var cadastro = _cadastroService.GetbyId(cadastroId);
            cadastro.Removido = !cadastro.Removido;
            _cadastroService.Update(cadastro);
            TempData["sucesso"] = "Paciente excluído com sucesso!"; 
            return RedirectToAction("Index");
        }

Page Excluircadastro.cshtml

<h3>Você tem certeza que deseja deletar este Paciente?</h3>
    <dl class="dl-horizontal">
        <dt>
            <a style="color: #000000"><b>Nome</b></a>
        </dt>
        <dd>
            @Html.DisplayFor(model => model.inpac)
        </dd>

        <dt>
            <a style="color: #000000"><b>Data Nascimento</b></a>
        </dt>
        <dd>
            @Html.DisplayFor(model => model.dtnasc)
        </dd>

        <dt>
            <a style="color: #000000"><b>Código prontuário</b></a>
        </dt>
        <dd>
            @Html.DisplayFor(model => model.pront)
        </dd>
    </dl>

    @using (Html.BeginForm())
    {
       @Html.AntiForgeryToken()
        <input type="hidden" name="cadastroId" value="@Model.CadastroId" />
        <div class="form-actions no-color">
            <button type="submit" class="btn btn-danger">Deletar</button> |
            @Html.ActionLink("Voltar à Lista", "Index")
        </div>
    }

when I click the Delete button, it keeps entering this if

var excluirCadastro = _cadastroService.GetByPatientId(id, hospitalId);

            if (excluirCadastro != null && excluirCadastro.HospitalId == hospitalId)
            {
                return View(excluirCadastro);
            }

as much as when I enter the page and as much as when I click on delete, it is always entering this if, someone could help me?
I wanted that when I clicked on delete he would send this patient to a page set by me and update the variable Removed to 1.
and the class I created Deletarpacient I’m not getting into it

2 answers

2


As you have not put the database you are using I will show you via one of the solutions.

// The Three Parts of a LINQ Query:
    //  1. Data source.
    int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

    // 2. Query creation.
    // numQuery is an IEnumerable<int>
    var numQuery =
        from num in numbers
        where (num % 2) == 0
        select num;

    // 3. Query execution.
    foreach (int num in numQuery)
    {
        Console.Write("{0,1} ", num);
    }

Instead of number put your class where removed != 1 if it is via SQL database would be something like this Select * from cadastros where removido != 1

0

hello, in my applications, performing lambda, is very simple. if you are using Entity framework, do it as follows? assign a field to identify which has been removed. then the following:

using(dbcontext db = new dbcontext(){
var RegDelete = db.tabela.where(s=> s.id == id).firstordefault();
if(regdelete != null){
    regdelete.removido = true;
}}
 db.savechanges();

If you want more treatment you can use a Try catch and capture the possible error;

  • sorry, I’m new and I don’t know how to put the code formatted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.