I am working with ASP.NET MVC, I am creating my views from scratch to pure html, creating an empty view creates this for me.
@{
ViewBag.Title = "Proveedor";
}
<h2>Proveedor</h2>
But when creating html code I do this
@{
ViewBag.Title = "Proveedor";
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<h2>Proveedor</h2>
</body>
</html>
I would like to know if the part of the ViewBag goes outside the html or goes inside and if it goes inside, in which part does it go?
The
ViewBag
can go anywhere in your file.cshtml
, except for the@section
and is basically defined as a dynamic object that serves as an interaction between theController
and theView
for passing temporary information from one to the other.It is important to mention that the
ViewBag
can only be used as a direct interaction between theController
and theView
, and it will goreciclando
away between future interactions.On the side of
Controller
, the use and assignment is:Now, to read it from the side
View
directly into an HTML table (as an example):References: Official documentation page.
The
ViewBag
is a global variable, if in your partial view_LayoutView
you have something like this:your code is correct and on each page you load your
<title>
will be modified where it would be best to remove the tags<title>
from your view this is just one more line that will not be necessary.If you don't have one defined
_LayoutView
, it would be best to deleteand leave
or if you prefer to leave it
Therefore, answering your question:
I would like to know if the part of
ViewBag
goes outsidehtml
or inside and if it goes inside, what part does it go in?The position where you put it does not matter, but it is recommended to leave it at the beginning since you can identify which view you are modifying, as I mentioned before if you do not use a
_LayoutView