Pass view data to the controller

0

I have a "a" tag in another view and another driver that is not from the client driver

<div class="col-lg-4 col-md-6">
            <a asp-action="Cliente" asp-controller="Cliente" class="">@item.Razonsocial</a>
        </div>

I thought of the ASP Helpers to pass something to it as a parameter.

How do I pass as parameter the @ item.name from the X view to the client driver?

public async Task<IActionResult> Cliente(string nit)
    {
        if(nit == null)
        {
            ViewBag.Message = "Debe ingresar Nit o Nombre del cliente";
            return View("Index");
        }

        var a = await _clienteService.GetClientesAsync(nit);

        if (!a.Any())
        {
            ViewBag.Message = "El cliente no existe o no pertenece a su zona de distribución";
            return View("Index");
        }

        return View("Index", a);
    }

This is the controller that receives it; if it receives a list, in the view it shows me some data, if it receives a single data, it shows me the client's data, that's what I want when I pass it the name

    
asked by Sebastian Rico 05.03.2018 в 23:26
source

3 answers

0

I leave another example that can help you

 public class JediController : Controller
{
    [Route("Jedi/{name}")]
    public IActionResult Details(string name)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentException("The name is required"); 
        }

        var jedi = new Jedi()
        {
            Id = 1,
            Name = name
        };
        return View(jedi);
    }
}

And in the view the anchor with tagHelper de route

@model Starwars.App.Web.Entities.Jedi
@{
 ViewData["Title"] = "Details";
 Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Details</h2>

Jedi: <strong>@Model.Name</strong>


  <ul>
      <li>
          <a asp-controller="Jedi"
             asp-action="Details"
             asp-route-name="@Model.Name">
              @Model.Name
          </a>
      </li>
   </ul>
 Another examples:
  <ul>
      <li>
          <a asp-controller="Jedi"
             asp-action="Details"
             asp-route-name="Yoda">
              Yoda
          </a>
      </li>
      <li>
          <a asp-controller="Jedi"
             asp-action="Details"
             asp-route-name="Luke Skywalker">
              Luke Skywalker
          </a>
      </li>
  </ul>

Links that can help you

answered by 06.03.2018 / 00:46
source
1

According to what I see, this can be useful to you

<a asp-controller="Cliente" 
   asp-action="Detalle" 
   asp-route-id="@item.nombre ">
@item.Razonsocial</a>

This generates:

<a href="/Cliente/Detalle/AlgunNombreAqui">Aqui va alguna razon social</a>
    
answered by 05.03.2018 в 23:43
0
[Route("Cliente/{nit}")]
    public async Task<IActionResult> Cliente(string nit)
    {
        if(String.IsNullOrEmpty(nit))
        {
            ViewBag.Message = "Debe ingresar Nit o Nombre del cliente";
            return View("Index");
        }

        var a = await _clienteService.GetClientesAsync(nit);

        if (!a.Any())
        {
            ViewBag.Message = "El cliente no existe o no pertenece a su zona de distribución";
            return View("Index");
        }
        return View("Index", a);
    }

That's how it worked for me, thank you very much everyone for the contributions.

    
answered by 06.03.2018 в 02:27