Link calling method in Controller - C# MVC

Asked

Viewed 936 times

0

I have a Mysql table that is shown on a page of my project:

foreach (var item in Model)
{

   <tr>
       <td>@Html.DisplayFor(modelItem => item.id)</td>
       <td>@Html.DisplayFor(modelItem => item.nomeMarca)</td>
       <td>
          <a>Deletar</a> &ensp;
          <a>Renomear</a> 
       </td>
   </tr>

I want when the user clicked on the link with the "Delete" text to send the item (from the "Mark) template to the Controller:

[HttpGet]
    public ActionResult RemoveMarca(Marca m)
    {
        MarcasDAO marca = new MarcasDAO();
        int i = marca.deletaMarca(m);
        if (i > 0)
        {
            ViewBag.Message2 = "Marca removida com sucesso!";
            return AddMarca();
        }
        else
        {
            ViewBag.Message2 = "Erro ao remover a marca";
            return AddMarca();                    
        }
    }

In "mark.deletaMarca(m)" you have the following code to remove the tag from the table:

public int deletaMarca(Marca m)
    {
        try
        {
            comando.CommandText = "delete from Marca where idMarca = @id";
            comando.Parameters.AddWithValue("@id", m.id);
            comando.Connection = con_sql;
            int i = comando.ExecuteNonQuery();
            comando.Dispose();
            objcon.Desconectar();
            return i;
        }
        catch (Exception)
        {
            throw;
        }
    }

But I have no idea how to make it work. I’ve tried a few things but it’s always a mistake. Methods to add a Tag are ok and work through the site.

1 answer

2


In your view, in each Table Row, you can have...

   <tr>
      <% using Html.BeginForm("action","controller", new { Id = item.id }, FormMethod.Post); { %>
         <td>@Html.DisplayFor(modelItem => item.id)</td>
         <td>@Html.DisplayFor(modelItem => item.nomeMarca)</td>
         <td><input type="submit" value="Remove Marca" /></td>
      <% } %>
  </tr>

The method in the Controller that receives the ID...

[HttpPost]
public ActionResult RemoveMarca(Id id)
{
   int i = marca.deletaMarca(id);        
   return RedirectToAction("Index");
}

And deleteMarca can only receive the ID...

public int deletaMarca(Id id)
{
    try
    {
        comando.CommandText = "delete from Marca where idMarca = @id";
        comando.Parameters.AddWithValue("@id", m.id);
        comando.Connection = con_sql;
        int i = comando.ExecuteNonQuery();
        comando.Dispose();
        objcon.Desconectar();
        return i;
    }
    catch (Exception)
    {
        throw;
    }
}
  • Thank you, man, the way you’ve been going has worked out just the way I’d like. However, instead of creating a form for each row of the table, I put it to be a link like this: <a href="/Tag/Removermarca/@item.id"></a> and it worked too. Only the codes you gave me already gave to have a notion. Thanks!

Browser other questions tagged

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