Complete C# Tutorial

Views – Displaying Data with Razor (Beginner-Friendly Guide)

👋 Introduction

Welcome back, buddy! 😄
Now that you understand controllers, it’s time to learn Views, the part that shows information to the user.

If controllers handle the request, then views handle the display.
Think of views as the “beautiful page” your visitors see.
They show messages, tables, forms, photos — everything your website needs.

Views use a special engine called Razor, which helps you write HTML and C# together in the same file.
Sounds fun? It is. Let’s explore it together! 🚀

🔗 Helpful Extra Resource

If you want a deeper dive into Razor basics, you can check this detailed guide:


👉 Complete Razor Tutorial: https://www.completecsharptutorial.com/legacy/razor-tutorial/

 

This page explains old Razor concepts in a simple way and can help reinforce your understanding.

🎓 Understanding Views

Imagine your controller is a teacher explaining something.
But the board where students read information? That’s the view.

So whenever a controller says “Hey, show this data!”, the View displays it.
That’s why views are the face of your .NET Core application.

Razor adds extra power by letting you write C# inside HTML using the @ symbol.
It makes your page smart without making it complicated.

💡 Your First Razor View

Let’s make a simple view.

Step 1: Controller

				
					public class HomeController : Controller
{
    public IActionResult Message()
    {
        return View();
    }
}
				
			

Step 2: View File

Create:
📄 Views/Home/Message.cshtml

Add:

				
					<h1>Hello from Razor View!</h1>
<p>This page is created using Razor.</p>
				
			

Now visit:
👉 /Home/Message

Boom! Your first Razor View is live. 🎉

🌈 Displaying Data with Razor

Let’s pass data from controller to view.

Controller:

				
					public IActionResult Greet()
{
    ViewBag.Name = "Prashant";
    return View();
}
				
			

View (Greet.cshtml):

				
					<h2>Hello, @ViewBag.Name 👋</h2>
<p>Welcome to Razor Views!</p>
				
			

When you open /Home/Greet, your name appears instantly.
Feels magical! ✨

⭐ A Bit More Razor Magic

✔️ Loop Example:

				
					@for(int i = 1; i <= 3; i++)
{
    <p>Number: @i</p>
}
				
			

✔️ Condition Example:

				
					@if(DateTime.Now.Hour < 12)
{
    <p>Good Morning! ☀️</p>
}
else
{
    <p>Good Evening! 🌙</p>
}
				
			

You can mix HTML and C# easily. Razor keeps everything clean and smooth.

🧭 Next what?

Nice job! You now know how views show data and how Razor works behind the scenes.
That’s a big achievement! 🎉

Now let’s move to the next exciting chapter.

👉 In the next chapter, you will learn “Models – Handling Data and Logic”.

This is where your application becomes smart and organized.
See you in the next lesson! 😄✨

Leave a Comment

16 − 5 =

Share this Doc

Views – Displaying Data with Razor

Or copy link