Create a view from scratch

6

I'm working with ASP.NET MVC, I'm creating my views from scratch to pure html, creating an empty view creates me this.

@{
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 it goes?

    
asked by Pedro Ávila 13.02.2017 в 17:29
source

2 answers

3

The ViewBag is a global variable, if in your partial view _LayoutView you have something like this:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    @RenderBody()

</body>
</html>

your code is correct and on each page that you upload your <title> will be modified where it would be best to eliminate the <title> of your view this is just one more line that will not be necessary.

If you have not defined a _LayoutView it will be best to eliminate

@{
    ViewBag.Title = "Proveedor";
}

and leave

<title>Proveedor</title>

or if you prefer to leave it

<title>@ViewBag.Title</title>

So answering your question:
Would you like to know if the% share% goes outside the ViewBag or goes inside and if it goes inside where does it go?

It does not matter where you put it, but it is advisable to leave it at the beginning since you can identify which view you are modifying, as mentioned above if you do not use a html

    
answered by 13.02.2017 / 17:59
source
3

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 the Controller and the View for the step of temporary information from one to the other.

It is important to mention that the ViewBag can only be used as a direct interaction between the Controller and the View , and this will go reciclando between future interactions.

On the Controller side, the use and assignment is:

public ActionResult Index()
{
    ViewBag.MyMessageToUsers = "Hello from me.";
    ViewBag.AnswerText = "Your answer goes here.";
    return View();
}

Now, to read it on the View side directly in an HTML table (by way of example):

<tr>
    <td>MyMessageToUsers: @ViewBag.MyMessageToUsers </td>
</tr>

References: Page of the official documentation.

    
answered by 13.02.2017 в 17:57