Create ASP.NET MVC Project
On the Visual Studio, create new ASP.NET MVC Web Application project
Select Empty Template and Core Reference is MVC
Create Controller
In Controllers folder, create new controller named DemoController.cs pass values to View as below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LearnASPNETMVCWithRealApps.Controllers
{
public class DemoController : Controller
{
public ActionResult Index()
{
ViewBag.age = 20;
ViewBag.fullName = "Kevin";
ViewBag.status = true;
ViewBag.price = 4.5;
ViewBag.birthday = DateTime.Now;
return View();
}
}
}
Create View
In Views/Demo folder, create new razor view named Index.cshtml as below:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
Age: @ViewBag.age
<br />
Full Name: @ViewBag.fullName
<br />
Status: @ViewBag.status
<br />
Price: @ViewBag.price
<br />
Birthday: @ViewBag.birthday.ToString("MM/dd/yyyy")
</body>
</html>
Structure of ASP.NET MVC Project
Run Application
Access Index action in Demo controller with following url: http://localhost:49328/Demo/Index
Output
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- ASP.NET MVC Framework Unleashed
- Programming Microsoft ASP.NET MVC (3rd Edition) (Developer Reference)
- Pro ASP.NET MVC 5 Platform
- Pro ASP.NET MVC Framework
- Professional ASP.NET MVC 5