MVC: parameter in URL is not being passed to controller

Asked

Viewed 135 times

1

I am running the Action below, but the parameter "Artigooucategoria" is not being passed to the Controller. I am working with Asp.net core 2.0.

public async Task<IActionResult> Buscar(string ArtigoOuCategoria)
{
    ...
}

The Action call is below:

 <div class="card mb-4">
        <div class="card-body">
            <h4 class="card-title">Categorias</h4>
            @foreach (var item in Model.Categorias)
            {
                <a class="btn btn-light btn-sm mb-1" asp-controller="Artigo" asp-action="Buscar/@item.Descricao">@item.Descricao</a>
            }
        </div>
    </div>

The Action call was also made as follows:

<a href="Artigo/Buscar/@item.Descricao">@item.Descricao</a>

Includes the route instructions in the project’s startup.Cs file:

  routes.MapRoute(
          name: "buscar",
          template: "Artigo/Buscar/{ArtigoOuCategoria?}",
          defaults: new { controller = "Artigo", action = "Buscar" });
  • Your route is set before of the standard route?

  • Not this one, I tried to change the order to see if there would be any difference, but I noticed that.

  • ArtigoOuCategoria is typed on your route?

  • No.. the parameter is string type but I did not specify its type on the route

3 answers

2

Gleison,

Solution 1

Voce needs to put in the [Fromquery] way thus:

public async Task<IActionResult> Buscar([FromQuery] string ArtigoOuCategoria)

{ ... }

But to work Voce needs to call it so:

<a href="Artigo/[email protected]">@item.Descricao</a>

Solution 2

State your method so

[HttpPost("Buscar/{ArtigoOuCategoria}")] 
public async Task<IActionResult> Buscar(string ArtigoOuCategoria)
  • 1

    Hello Paul.. I tried all the solutions you sent. But they didn’t work. The URL of Solution 1 is the customization I don’t want to perform. That way it works.

  • 1

    just remembering that the solution two, the method is as Post and the name Search, ie in the API, Voce need to Call "Your API Address"/Search/STRING

  • Paul, when I changed the parameter name to id it worked. But with the parameter name Artigooucategory it didn’t work. Valew by help.

  • 1

    Good, I’m glad I helped =D

0

Friend, first, to call this action, it must have the Httpget attribute to access this way with the href of a button.

Second: to pass this parameter to the action, use the button as follows:

<a class="btn btn-light btn-sm mb-1" asp-controller="Artigo" asp-action="Buscar" asp-route-artigooucategoria="@item.Descricao">@item.Descricao</a>

Your action should be in this pattern:

[HttpGet]    
public async Task<IActionResult> Buscar([FromRoute] string ArtigoOuCategoria)
{
    ...
}

Source: Working with Forms

  • I did the test, but unfortunately the parameter keeps coming null.

  • Take the route statement in Startup.Cs, this is done by convention, and change the name of the Artigooucategory parameter to articleOuCategory

  • Gustavo, when I changed the parameter name to "id" it worked. But with the parameter name Artigooucategory it didn’t work.

0

Make sure your default route is configured:

routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");

and change your link to this, using the attribute asp-route-:

<a asp-controller="Artigo"
   asp-action="Buscar"
   asp-route-ArtigoOuCategoria="@item.Descricao">Edit</a>

See that the ArtigoOuCategoria after the asp-route must be equal to the parameter name of your, action, so null is coming for you.

Tip: put a break point on your method Fetch, and on Google Crhome, press crtr + Swift + j, and go to the Network tab, and then click on the button to call the Search action, you’ll see that your action call will appear by clicking on it, you can go to the Headers sub-tab, and check what you’re passing to your action.

  • Cassio, when I changed the parameter name to id worked. But with the parameter name Artigooucategory it did not work.

Browser other questions tagged

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