Pass parameters through the url

Asked

Viewed 517 times

1

I’m trying to pass a parameter of my view to my controller by the url and I’m not getting it, as you can see in the code below I’m using viewbag, I don’t know if it’s an error on the route or something else, if you want any other part of the code just ask:

@using (Html.BeginForm("Consultar", "Tce", FormMethod.Get, new { @id = "oForm" }))
{
    <input name="pIdTce" type="text" maxlength="200" id="pIdTce" class="form-control input-sm" placeholder="Id do Tce" value="@ViewBag.IdTce">
}

 @Html.ActionLink("Consultar", "Consultar", "Tce", new { id = @ViewBag.IdTce }, new { @class = "btn btn-primary btn-sm" })

Follows the controller:

using SgePrefeituraJoao.Modelo;
using System.Threading.Tasks;
using System.Web.Mvc;
using SgePrefeituraJoao.Models;
using SgePrefeituraJoao.Web.Mapeamento;
using SgePrefeituraJoao.Repositorio;

namespace SgePrefeituraJoao.Controllers
{
    public class TceController : Controller
    {

        private readonly SgePrefeituraJoaoContext _context = new SgePrefeituraJoaoContext();
        public ActionResult Tce()
        {
            return View("Tce");
        }
        [HttpGet]
        public async Task<ActionResult> Consultar(int id)
        {
            var _TceRepositorio = new TceRepositorio();
            ViewBag.pIdTce = id;

            var _TCE = await _TceRepositorio.buscarTce(id);

            var _TceView = MapperFacade.MapperConfiguration.Map<TceView>(_TCE);

            //var _TceView = MapperFacade.MapperConfiguration.Map<TceView>(await _TceRepositorio.buscarTce(id));

            return View("Consultar", _TceView);
        }
    }
}

2 answers

3

In the controller, the line is like this:

ViewBag.pIdTce = id;

And in the view,

new { id = @ViewBag.IdTce }

Missed the p!

0

Whenever you need to pick up something coming by route, you need to specify on [HttpGet]. Do the following in the method developer:

[HttpGet("{id:int}")]
public async Task<ActionResult> Consultar(int id)

Browser other questions tagged

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