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.
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.
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.
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.
Comments
Post a Comment