In this article, we’ll discuss Views in ASP.NET MVC framework. In MVC, the views we refer to your views can be PDF, Html, text or anything else. But in a web application most of the time it is a team which is redundant and we see it’s razor engine supports it in a nice way.
here, we added this index action.
It is what we have in the first place. the index action uses the View method let’s say read and check it.
It will happen if we tested as it is in the browser.
If we go to index action and press and there we get these error shows.
Let’s go back to our solution explorer and create a view.
Then we can select the Add new folder we need to name it a test because the name of these folders must be the same as the controller.
Now, we can right-click on test for them and select view then we see this dialog box.
Here, we need to change the name in the dialog box which is the name of our action than for the index to do our testing and we see a framework generated in the index.cshtml for us.
It’s something very similar to simple ASP.NET a page to do our test thing.
here, we added these H1 tags.
Now, let’s see the output in the browser.
so far we have reached the point that we created a simple HTML of views. usually, a view has a header and footer so we added this mock header and footer looking at this code.
We can say it’s good for one single page application but for a multi-page application we can’t say the part and the following part is going to be repeated in all pages.
As a programmer, we know that we should not repeat ourselves layouts is something that helps us in the situation if we open the solution explorer and we use Folder. We have a shared folder.
Let’s create our own Layout to do so we can right-click on the shared folder and select at the views in Add view dialog box.
here we changed the name to _TestLayout and click the Add button.
We have and we use similar to other views and nothing more.
Let’s go back to the layout and add some code into _TestLayout.cshtml.
Now, here in our layout heading part is going to get changed page by page so we can replace it with at @RenderBody() but in the views, everything starts with. @ sign is the razor syntax which means we are going to put C-Sharp quotes RenderBody() is the method that placed the result of value inside.
Now, we need some changes in the index.cshtml and also we can change the layout from all to on the _TestLayout. Layout the name of our day out without the extension.
Now, we’ll see you take the test out as is the layout and then render a result of this views. which is now a simple Layout where RenderBody code that saves everything and tests it in browser to get Sure it still works.
Define script section
The webpages may need to put some scripts, before the end of the body. For such cases we can use sections, a section can be defined like this here.
we define a section called scripts a section that can be optional or mandatory here because the value of required is false. It is optional.
Now, let’s add this code to define a section called script.
By this section, we have defined in view the code inside this section will be placed in RenderScript in the layout. as before the body we define title in viewdata and tested in the browser.
Let’s, we refreshed the page when we get the alert.