Pass parameters from a view to a controller using href in Visual Studio

0

I need help to pass a parameter from a view to a controller. The problem I have is that when I send the data back to me a null data and not the one I am sending, it is assumed that through the "href:" I should be able to send the variable to the controller. This is my view:

@model List<Model.Cliente>
@{
ViewBag.title = "Cliente";
}
   <h2>Cleintes</h2>

    <table class="table table-striped">
        <thead>
            <tr>
                <th style="width:50px;">Id</th>
                <th style="width:100px;">RUT</th>
                <th>Razsoc</th>
                <th style="width:200px;"></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var a in Model)
            {
            <tr>
                <td>@a.Id_clien<td>
                <td>@a.Rut</td>
                <td>@a.Nombre_clien</td>
                <td class="text-right">
                    <a class="btn btn-default btn-success" href="~/Menu/Ver/@a.Rut" title="Visualizar">
                        <i class="glyphicon glyphicon-eye-open"></i>
                    </a>
                    <a class="btn btn-default" href="~/menu/crud/@a.Rut" title="Editar">
                        <i class="glyphicon glyphicon-pencil"></i>
                    </a>
                </td>
            </tr>
            }
        </tbody>
    </table>

And this is my controller:

using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sales.Controllers
{
public class MenuController : Controller
{
    private Cliente cliente = new Cliente();

    public ActionResult Inicio()
    {
        return View(cliente.Listar());
    }

    public ActionResult Ver(string rut)
    {
        return View(cliente.Obtener(rut));
    }
}
}
    
asked by g.cifuentes 20.10.2018 в 04:35
source

1 answer

1

In the href use the helper Url and the Url.Action() method.

This takes as parameter the action, the controller (without the word "Controller") and the routeValues which is an object with the parameters that you will pass to the action. It has 9 overloads plus the method you can review, but basically it would be something like this:

<a href="@Url.Action("Ver", "Menu", new { rut = a.Rut })" title="visualizar">
   <i class="glyphicon glyphicon-eye-open"></i>
</a>
    
answered by 20.10.2018 / 05:33
source