Remove undisclosed user from the database

2

I need your help, I created two classes inside my Controller, where I can pull a user from one page and send it to another. As if you were deleting but not deleting from the database.

I created a variable in my database called Removed where I think it would be better to work when Removed is = 1 it transfers this user to another page , which in this case would be like I was deleting the user from that page

follow 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");
        }

ExcludeCadastro.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 on the Delete button, it always goes into this if

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

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

Both when I enter the page and both when I click delete, it is always entering this if, could someone help me? Home I wanted it when I clicked on deleting it it would send this patient to a page defined by me and would update the Removed variable to 1.
and the class I created Deleting Patients I'm not getting into it

    
asked by anonymous 14.06.2017 / 20:48

2 answers

2

As you have not put the database you are using I will show via linq 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 place your class where removed! = 1 if it were via SQL database it would be something like this   Select * from cadastros where removido != 1

    
14.06.2017 / 21:29
0

Hello, in my applications, I realize lambda, it's very simple. if you are using the entity framework, do the following? assign a field to identify which was 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 catch the possible error;

    
15.06.2017 / 05:14