It turns out that I have a page which has a navbar, I added this navbar to the _layout
howPartialView
CONTROLLER
[ChildActionOnly]
public PartialViewResult BarraNavegacion()
{
if (Session["cambio"] == null)
{
string fechaActual = DateTime.Now.ToString("dd/MM/yyyy");
string xml = indicadorEconocomico.ObtenerIndicadoresEconomicosXML("318", fechaActual, fechaActual, "xxx", "S");
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
Session["cambio"] = json;
ViewBag.dolar = Session["cambio"].ToString();
}
return PartialView("~/Views/Shared/_navbar.cshtml");
}
I am saving what I want to show in a ViewBag
, from here when I show the part of navbar
it shows me theViewBag
NAV BAR
<li class="nav-item d-none d-sm-inline-block">
<h5 class="cambioHoy">Tipo de cambio: @ViewBag.dolar</h5>
</li>
When I run the web page, it shows me the value of the dollar, when I navigate to another view, the information is lost (that is, nothing is shown), how can I show the navbar info in all views? (taking into account that it navbar
is a PartialView
)
In your case I see that the "exchange rate" is being stored in the session
Session["cambio"] = json;
.Then you would not need to use the
ViewBag
to access the "exchange rate" in the View you want. Simply use the value you have stored in the session directly in the View.Your code would look like this: