Pass parameters to the Controller via @Html.Action

2

I need to render one view inside another, but this rendered view needs to receive and display data from the database, so I'll use the @Html.Action for this, but I need to pass a parameter to the method on the controller fetch the data in the database with based on this parameter and weighting the View with the data. The problem is that the way I did the parameter is not past, it always gets null.

View:

    @Html.Action("ExibirCliente", "Cliente", new {idcliente = Model.ID})

Controller:

    [Authorize]
    public ActionResult ExibirCliente(int idcidade)
    {
        Cliente c = new Cliente();
         c = c.BuscarCliente(idcidade);
        return PartialView("~/Views/Shared/_ViewCLiente.cshtml", cep);
    }
    
asked by anonymous 29.04.2015 / 19:06

2 answers

2

The parameter passed in the anonymous object must be exactly the same in the Action signature. See that:

@Html.Action("ExibirCliente", "Cliente", new {idcliente = Model.ID})

The parameter is idcliente , whereas in Action :

public ActionResult ExibirCliente(int idcidade)

Obviously the Model Binder will not do the correlation and the parameter will be null.

Now, if you change to:

@Html.Action("ExibirCliente", "Cliente", new {idcidade = Model.ID})

It will work.

    
29.04.2015 / 19:12
3

The parameter name in View and Controller must be the same, so you are only getting null . Just change your View to

@Html.Action("ExibirCliente", "Cliente", new {idcidade = Model.ID})

Since your Controller expects to receive a parameter called idcidade .

    
29.04.2015 / 19:11