As a first option I am trying to create a strongly typed partial view. It would render inside of inside me _Layout.cshtml
since the idea is that this partial view _UserInfoPartial.cshtml
will be seen in all other views.
The second would be that from a parent view, for example from the home/index
, render the aforementioned view.
How could I do to pass another model
to the daughter view?
Partial view model:
public class DatosUsuario
{
public short Compania { get; set; }
public Legajo LegajoUsuario { get; set; }
public Funcion Funcion { get; set; }
public string UsuarioNT { get; set; }
}
Parent view model:
public class Visita
{
public short Compania { get; set; }
public Local Local { get; set; }
public DateTime Fecha { get; set; }
public DateTime Periodo { get; set; }
public Legajo LegajoGerente { get; set; }
public Color Color { get; set; }
public DateTime FechaCierre { get; set; }
}
This is my_Layout.cshtml
<body>
<div class="container body-content">
@RenderBody()
<hr />
@Html.Partial("_UserInfoPartial") @*aqui renderizo mi vista parcial*@
<footer>
<p>© @DateTime.Now.Year</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
I don't know why option to go, but it seems to me the most accurate to do it in it layout
since it should be shown in all views.
For this case it would be better to use a child action instead of just a partial view
in a controller , say Home :
(
[ChildActionOnly]
it is not mandatory to call it, it is just so that it is not called directly as a normal action)and then in
_Layout
:@Html.Action
write in the layout the result of invoking the indicated action, and there you can specify the view and model you want to useYou can also put the partial view's model inside the parent view's model, and thus load it all at once. If you have to load it in all views, create a base model that all models inherit.