View Result in Controller Sample in MVC

View Result in Controller Sample in MVC

Introduction

This article shows how to use view result in a Controller in MVC.

Step 1

Create a MVC project from the "Empty" template.

Right-click on "Controllers" and select "Add" >> "Controller...".

image.png

Step 2

Select "MVC 5 Controller - Empty" to add an empty Controller.

Click on the "Add" button.

image.png

Step 3

Name the Controller as in the following:

image.png

Step 4

Now we need to create a view.

Right-click on "Index" and select "Add View...".

image.png

Step 5

Name the view and select "Empty (without model)" as the template.

Click on the "Add" button.

image.png

Step 6

Add a title in the index page.

image.png

Step 7

ViewResult represents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object. View() creates an object that renders a view to the response.

image.png

Step 8

Now we need to create a view.

Right-click on "About" and select "Add View...".

image.png

Step 9

Name the view and select "Empty (without model)" as the template.

Click on the "Add" button.

image.png

Step 10

Add the title in the about page.

image.png

Step 11

Add html.actionlink for about.cshtml.

image.png

Step 12

Run the project, click on the about link and you will see the about page rendered in the browser. image.png

<< Day 10

>> Day 12