How to create and call View in ASP.net MVC


In this article I'm going to explain how to create a View and how to return the ActionResult View from Controller with small example. This article will helpful to those who are beginners to ASP.net MVC.

How to create and call View in ASP.net MVC:



Summary:


In this article I'm going to explain how to create a View and how to return the ActionResult View from Controller with small example. This article will help those who are beginners to ASP.net MVC.

Follow below steps to accomplish the task.

Step 1 : Create View

Select Views in solution explorer then right click on Views and then Add new View.

1

Step 2 : In Add View dialog box give a name of View and then uncheck the use a layout checkbox and then click on Add button.

2

It will add a new View inside "Views/Demo" folder in solution explorer. Views are associated with the particular Controller is placed inside a special folder. This special folder is called as a ControllerName, What are all the Views are a available inside that folder is belongs to against that particular Controller only.

3

Step 3 : Add a content to View

Open DemoView.cshtml and add content to that as like below.


@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>DemoView</title>
</head>
<body>
<div>
Welcome to ASP.net MVC 5
</div>
</body>
</html>


Step 4 : Create New Action method

Add a new Action method inside DemoController as follows.

public class DemoController : Controller
{
//
// GET: /Demo/
public ActionResult Index()
{
return View();
}

public ActionResult GetView()
{
return View("DemoView");
}
}


Result:

And run the application like below format.http://Instance/ControllerName/MethodName

4


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: