Doubt with passing Parameter Asp.net MVC

Asked

Viewed 1,537 times

2

Calling the controller is no longer calling the Actionresult (Solved) 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>

in the controller:

        // GET: Operacao
        public ActionResult Index(int id)
        {
            //pega a opção selecionada para trazer as opções
            int opcao = id;
            return View();
        }
  • Put the view name and folder where it is, please?

  • <a href="@Url.Action("Index", "Operation", @Html.Displayfor(c => item.idmenu)"class="list-group-item">@Html.Displayfor(c => item.descricaomenu)</a>

  • @Ricardo this in the question,

1 answer

1


You have some very serious errors in this code.

First, you’re expecting a Model in his ActionResult, but is not passing a Model for her.

Second, you are using [HttpPost] and is looking to pass via GET with the @Url.Action.

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

Now we go the solution.

If you want to pass a model, or you do it by ajax or send by post (via form). However, in your code it seems to me that you’re just trying to pass the idMenu. If so, just change your controller and your @Url.Action to receive and pass the 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 his @Url.Action, put this way:

<a href="@Url.Action("Index", "Operacao", new{idMenu = item.idmenu})" class="list-group-item">@Html.DisplayFor(c => item.descricaomenu)</a>
  • It all worked out, I made the code change posted

  • @itasouza do not forget to accept the answer if it helped you solve your problem

  • @Richarddias, yes, of course, I appreciate the reminder!

Browser other questions tagged

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