Question with asp.net MVC parameter passing

2

You are calling the controller but you are not calling ActionResult, I have my view:

@model IEnumerable<Generico.Dominio.TB_MENU>

@{
    ViewBag.Title = "Index";
}

@Html.Partial("_navbarInterno")

@Html.Partial("_PartialLogin")




<div class="list-group">
    <a href="#" class="list-group-item active">
        Seleccione una opción
    </a>

    @if (Model.Count() > 0)
    {
        foreach (var item in Model)
        {
            <a href="/Operacao/Index/@item.idmenu" class="list-group-item">@Html.DisplayFor(c => item.descricaomenu)</a>
        }
    }

</div>

no controller:

        // GET: Operacao
        public ActionResult Index(int id)
        {
            //pega a opção selecionada para trazer as opções
            int opcao = id;
            return View();
        }
    
asked by anonymous 28.12.2015 / 23:18

1 answer

1

You have some very serious errors in this code.

First, you're expecting Model in your ActionResult , but you're not passing Model to it.

Second, you are using [HttpPost] and are wanting to pass via GET to @Url.Action .

Third, if you want to pass idMenu as a parameter, do not use @Html.DisplayFor(c => item.idmenu) , use only item.idmenu .

Now let's get the solution.

If you want to pass a model, either you do this by ajax or send by post (via form ). However, in your code it seems to me that you want to pass only idMenu . If so, just change your controller and its @Url.Action to receive and pass idMenu , respectively. It would look like this:

  //Retire o [HttpPost]
    public ActionResult Index(int idMenu)//O nome aqui é apenas para estudos, pode ser o que quiser.
    {
        int CodigoOpcao = model.idmenu;



       //aqui eu verifico se chamo a modalidade ou outra opção
       //preciso 
        return View();
    }

And in your @Url.Action , put it this way:

<a href="@Url.Action("Index", "Operacao", new{idMenu = item.idmenu})" class="list-group-item">@Html.DisplayFor(c => item.descricaomenu)</a>
    
29.12.2015 / 00:37