Getting registration id

Asked

Viewed 137 times

1

I have a table with multiple records and a link to delete, how do I click this link to pass the id value of the record I want to delete ? Code from my view:

@model IList<Financas.Entidades.Usuario>

@Html.ActionLink("Novo Usuário", "Form")
<table class="table table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Nome</th>
            <th>E-mail</th>
        </tr>
    </thead>
        <tbody>
            @foreach(var usuario in Model)
            {
                <tr>
                    <td>@usuario.Id</td>
                    <td>@usuario.Nome</td>
                    <td>@usuario.Email</td>
                    <td>@Html.ActionLink("Excluir","Excluir","Usuario")</td>
                </tr>
            }
        </tbody>
</table>

Controller:

    public ActionResult Excluir(int? usuario)
    {
            try
            {
                IList<Usuario> dados = usuarioDAO.GetById(usuario);
                return View(dados);

            }
            catch (Exception)
            {

                throw;
            }
        }

2 answers

0

Just spend like this:

Html.ActionLink("Excluir", "Excluir", "Usuario", new {id = usuario.Id}, null)
  • I did it the way you said, but it still comes null value for my controller...

  • passes the action signature in the controller

  • I edited and put the controller code in the post

  • switches to public Actionresult Delete(int id)

  • The last parameter calls Id. Then you have to have a parameter called Id in action for mvc to bind. and I took nullable from type.

  • I edited your controller in the post.

  • copies the edited action to your controller

  • But this method of yours excludes nothing. and is giving a getbyid to a list of users instead of to a user.

  • I need to list before deleting ? because in meo DAO is: public void Delete(User user) { context.Usuarios.Remove(user); context.Savechanges(); }

  • If it worked mark as answered.

Show 5 more comments

0


Let’s deal first with the mistakes in your code.

The error of the question is simple, you are not passing anything as parameter to Action. Just change to the following:

   <td>@Html.ActionLink("Excluir","Excluir","Usuario", new{usuario = usuario.Id})</td>

That way you’ll be passing one ID for the parameter usuario in his Action.

Now let’s make some improvements to your code?

To first:

I don’t know what’s in her usuarioDAO, but apparently you’re just listing a list of users by ID. At no time in your code is there an option to delete.

public ActionResult Excluir(int? usuario)
        {
                try
                {
                    IList<Usuario> dados = usuarioDAO.GetById(usuario);
                    return View(dados);

                }
                catch (Exception)
                {

                    throw;
                }
            }

Second:

If you don’t own one View delete confirmation, why leave the option to pass a ID null? If you are going to delete a user, some data has to be passed, that is, you can change the parameter of int? usuario for int usuario. That way you’ll be ensuring that you always have a value.

Third:

I would change your whole logic just for that:

public ActionResult Excluir(int id)
            Usuario usuario= db.Usuarios.Find(id);

            if(usuario == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);//Um erro aqui, pois o usuário não foi encontrado

            db.Usuarios.Remove(usuario);
            db.SaveChanges();
            return RedirectToAction("Indice");
}
  • Who excludes is my DAO class, where I implement: public void delete(User user) { context.Usuario.Remove(user); context.saveChanges(); }

  • @Desalex There is no reason for this. This kind of implementation has several answers here on the site, just look to understand better. However, that is not the focus of the question. Just use only the solution posted at the beginning of the answer that will solve your problem, that is, taking into account that your DAO class is correct.

  • got it, I just meant that whoever does the insertion and deletion control of my application is the DAO layer. But thanks for the clarification

Browser other questions tagged

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