Referencing Partial View and showing its data when loading in the parent view

1

Hi, create a partial view through a one-action method of a controller. When referencing the partial view from a parent view with the following code

@Html.Partial("~/Views/SubAutomovil/Create.cshtml")

The partial view is loaded in the parent view but it does not load the data sent by the controller. If I call this same view as any normal view using the following Code

<a asp-controller="SubAutomovil" asp-action="Create">

Show the view and load the data correctly. I suppose, then, that since the parent view uses a different controller than the partial view, the latter is not able to load its own data, maybe it does not. I have little learning ASP.NET Core (MVC6) so I have little experience. If someone already solves this please show me how I can solve it.

    
asked by Daniel 03.07.2017 в 08:13
source

1 answer

0

you can use the helper @ Html.Action ()

example: Let's say that you have in your controller a method that returns a partial view with the details of a worker, so this method must accept parameters such as the id or the name of the worker.

controller

 public ActionResult DetallesTrabajador(int? id)
    {

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Trabajador trabajador = db.Trabajadores.Find(id);
        if (trabajador == null)
        {
            return HttpNotFound();
        }
        return PartialView("_DetallesPartial", trabajador);
    }

in your partial view you show the worker's data and you will render it in the following way:

Assuming that your method is inside the Worker driver and you want to show the partial in the index view of the same driver. you call it like this:

Views / Worker / Index.cshtml

 @Html.Action("DetallesTrabajador", new { id = 27 }) 

you only specify the name of the action, since it is in the same controller it would be called ChildAction. If you want to call from another view that is in another controller

@Html.Action("DetallesTrabajador", "Trabajador", new { id = 27 })

Where "Worker Details" is the Action to be found in the "Worker" driver and as in both cases the worker's id is expected as a parameter because you pass it in the way shown. I hope it helps you

    
answered by 03.07.2017 / 14:27
source