View In Asp.Net MVC By Sagar Jaybhay

In this article you will able to understand what is View In Asp.Net MVC. How to Create View In MVC By Sagar Jaybhay

Views In Asp.Net MVC

The purpose of view in MVC to render the data in the format that the end-user want. If you want to return the view from the action method which you going to create by default MVC adds the same name to view as an action method name See below image.

View In Asp.Net MVC
View In Asp.Net MVC

Our application, we add a NameList method and by using the MVC dialog box we are going to add View. The name of the method and view name is the same is MVC default convention and this view is added Under the Views -> Home(Controller Name Folder)-> View See below image.

Create View In Asp.Net MVC
Create View In Asp.Net MVC

public ActionResult NameList()
        {
            var list=new List<string>()
            {
                "Sagar Jaybhay",
                "Ram",
                "Raghu",
                "Ravan"

            };
            ViewBag.list = list;
            return View();
        }

Above is Controller action method and below is the code for view.

@{
    ViewBag.Title = "NameList";
}

<h2>NameList</h2>

<h3>List Of Names</h3>

<br/>

<div class="row">
    <ul>
        @foreach (var str in ViewBag.list)
        {
            <li>@str</li>
        }
    </ul>
</div>

See the below image which is our output.

OutPut View In Asp.Net MVC
OutPut View In Asp.Net MVC

Also, one more important note in View if you want to switch between Html code and C# code you need to use @( at the rate) symbol. In the above example, we use ViewBag to pass data from controller to view. In the coming up tutorial I will explain in detail about ViewBag and ViewData which is a mechanism to pass data from controller to View.

GitHub – https://github.com/Sagar-Jaybhay/MVC5

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...

1 Response

  1. March 4, 2020

    […] The flow of MVC is when you hit URL it first go to the Controllers‘ action method, the controller will search for if particular request required data or not if yes then it gets from Model and then this model is the pass to View. […]

Leave a Reply

Your email address will not be published. Required fields are marked *