How to pass a parameter through the URL using Httpget in ASP.NET CORE?

Asked

Viewed 644 times

1

I have an application in ASP.NET CORE 2.0 where I did the following method in the controller [HttpGet] public IActionResult Index(int idLeilao) { ConsultaCarrosModel obj = new ConsultaCarrosModel(); ViewBag.ListaMarcas = obj.ListaMarcas("marcas.json"); return View(); }

When you click on a button passing this 'ID' in the url it is loaded with the ID I want but the value that arrives in the controller is always 0

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

As you can see the ID I passed in the URL is 4 but the one that arrived in the controller was 0(default), as if it did not arrive in the controller before loading the page

1 answer

3

Try using the Route attribute:

 [HttpGet("/Home/Index/{idLeilao}")]
 public IActionResult Index(int idLeilao)
 {
     ConsultaCarrosModel obj = new ConsultaCarrosModel();
     ViewBag.ListaMarcas = obj.ListaMarcas("marcas.json");
     return View();
 }

Routing for controller actions in ASP.NET Core

Browser other questions tagged

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