ASP.NET MVC 5 Helllo World
Tags: csharp
Published: 2019-10-12 02:13:17

Buiding a example simple with ASP.NET MVC5. we can see, many developer program ASP.NET MVC 5
Today, I will shared buiding way create project ASP.NET MVC 5, with program Visual Studio 2019
Everyone,can download here:https://docs.microsoft.com/en-gb/visualstudio/releases/2019/release-notes
Installing, note you tab open Individual Components, after then, you choose Class Designer and LINQ to SQL Tools

**Setup project ASP.NET MVC 5**
You create project
![ASP.NET MVC 5 Helllo World - hoanguyenit.com](https://thepracticaldev.s3.amazonaws.com/i/f27tt1wuay9g76u791pr.png)
You choose ASP.NET Web Application, let's go, you can need choose Version Framework
After create success project, you can add news HomeControler.cs file in Controllers directory
![ASP.NET MVC 5 Helllo World - hoanguyenit.com](https://thepracticaldev.s3.amazonaws.com/i/c37i6al2tjyxsmnydstv.png)
![ASP.NET MVC 5 Helllo World - hoanguyenit.com](https://thepracticaldev.s3.amazonaws.com/i/nxtg0x7b5vt39mco40q5.png)
![ASP.NET MVC 5 Helllo World - hoanguyenit.com](https://thepracticaldev.s3.amazonaws.com/i/n5im5l4alw4q9oua0usg.png)

Ok, Begin you create layout project
+ Create **Shared** folder in directory Views
+ Create **_LayoutPage1.cshtml** file in **Views/Shared** directory

![ASP.NET MVC 5 Helllo World - hoanguyenit.com](https://thepracticaldev.s3.amazonaws.com/i/fw5cdl1vi2givrrgm0aw.png)

you create layout razor success!, you open HomeController.cs, **click-right function index()-> choose add view**
![ASP.NET MVC 5 Helllo World - hoanguyenit.com](https://thepracticaldev.s3.amazonaws.com/i/78rfgrwixsmz4hieka66.png)

![ASP.NET MVC 5 Helllo World - hoanguyenit.com](https://thepracticaldev.s3.amazonaws.com/i/hokwloorjx1rz3964cgs.png)

You have new Index.cshtml file in **Views/Home/Index.cshtml** directory
Continue! you need to **Controllers/HomeController.cs** open it and configuration function index(), the folllowing below code
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC5_HelloWorld.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.title = "MVC5 - Hello World";
            return View();
        }
    }
}
```
We set ViewBage.title to insert a string of data or an array of data that we want it to be displayed outside of View
You can using LinQ, you can see the following below
```csharp
//HomeController.cs
var data = (from s in _db.users select s).ToList();
ViewBag = data;
return View()

//Views/Home/Index.cshtml
@foreach(var result in ViewBag.data){

  <span>@result.name</span>
  <span>@result.created_at</span>
}
```
The above is an example if you want to retrieve data in SQL SERVER, to display outside the View

Continue to you **Views / Home / Index.cshtml** and open up and edit the following:
```csharp
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutPage1.cshtml";
}

<h2>@ViewBag.title</h2>
```
That's it, you can Run the project and test it out!
Post:
+ [ASP.NET MVC 5 Hello World](https://hoanguyenit.com/aspnet-mvc5-hello-world.html)
+ [ASP.NET MVC 5 Create Shared Layout Razor & ViewStart](https://hoanguyenit.com/aspnet-mvc5-create-shared-layout-razor-and-viewstart.html)