Backend
Backend
namespace WelcomeApp.Controllers
{
public class WelcomeController : Controller
{
public ActionResult Greet()
{
ViewBag.Message = "Welcome to the MVC World!";
return View();
}
}
}
html
CopyEdit
@{
ViewBag.Title = "Welcome Page";
}
<h1>@ViewBag.Message</h1>
🖥️ This tells the screen to show the message: "Welcome to the MVC World!"
csharp
CopyEdit
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional
}
);
csharp
CopyEdit
routes.MapRoute(
name: "WelcomeRoute",
url: "greeting-page",
defaults: new { controller = "Welcome", action = "Greet" }
);
🚀 Now when someone types http://localhost:xxxx/greeting-page, they will see your welcome
message!
👉
● In the address bar, type:
http://localhost:1234/greeting-page (your number might be different)
=========================================================
===========================
Create an employee model using strongly typed views
csharp
CopyEdit
namespace EmployeeApp.Models
📓 This makes a blueprint for an employee with 4 things: Id, Name, Department, Age.
csharp
CopyEdit
using System.Web.Mvc;
using EmployeeApp.Models;
namespace EmployeeApp.Controllers
Id = 1,
Name = "Rajat",
Department = "IT",
Age = 25
};
return View(emp);
○ Name: Details
○ Click Add
html
CopyEdit
@model EmployeeApp.Models.Employee
<h2>Employee Details</h2>
<p><strong>ID:</strong> @Model.Id</p>
<p><strong>Name:</strong> @Model.Name</p>
<p><strong>Department:</strong> @Model.Department</p>
<p><strong>Age:</strong> @Model.Age</p>
🧑🎓 @Model is like saying: “Show me the data from the Employee notebook!”
arduino
CopyEdit
http://localhost:1234/Employee/Details
🎉 You’ll see:
makefile
CopyEdit
Employee Details
ID: 1
Name: Rajat
Department: IT
Age: 25
=========================================================
===========================
Write a program demonstrating ViewData,ViewBag, Temp data and session
variables
1. In Solution Explorer, right-click on the Controllers folder ➜ Click Add ➜ Controller.
csharp
CopyEdit
using System.Web.Mvc;
namespace DemoDataProject.Controllers
{
public class HomeController : Controller
{
public ActionResult FirstPage()
{
ViewData["MyViewData"] = "Hello from ViewData!";
ViewBag.MyViewBag = "Hello from ViewBag!";
TempData["MyTempData"] = "Hello from TempData!";
Session["MySession"] = "Hello from Session!";
return RedirectToAction("SecondPage");
}
💬 In simple words:
● We're putting messages in ViewData, ViewBag, TempData, and Session.
● Then we are sending the user to another page (SecondPage) to show those messages!
html
CopyEdit
<h2>Showing All Data</h2>
<p><strong>ViewData:</strong> @ViewData["MyViewData"]</p>
<p><strong>ViewBag:</strong> @ViewBag.MyViewBag</p>
<p><strong>TempData:</strong> @TempData["MyTempData"]</p>
<p><strong>Session:</strong> @Session["MySession"]</p>
arduino
CopyEdit
http://localhost:1234/Home/FirstPage
(Replace 1234 with your number)
csharp
CopyEdit
return RedirectToAction("SecondPage");
To this:
csharp
CopyEdit
return View("SecondPage");
vbnet
CopyEdit
ViewData: Hello from ViewData!
ViewBag: Hello from ViewBag!
TempData: Hello from TempData!
Session: Hello from Session!
====================================================================================
Write a program using Entity framework to provide facility to insert,
edit and delete
bash
CopyEdit
Install-Package EntityFramework
namespace StudentCRUDApp.Models
{
public class Student
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
csharp
CopyEdit
using System.Data.Entity;
namespace StudentCRUDApp.Models
{
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
}
xml
CopyEdit
<connectionStrings>
<add name="StudentContext"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=StudentDb;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
4. Set:
arduino
CopyEdit
http://localhost:1234/Students
====================================================================================
Write a program to send student information form controller to view
csharp
CopyEdit
namespace MyStudentApp.Models
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Class { get; set; }
}
}
🧠 This just says: “I have a Student with a Name, Age, and Class.”
csharp
CopyEdit
using System.Web.Mvc;
using MyStudentApp.Models;
namespace MyStudentApp.Controllers
{
public class StudentController : Controller
{
public ActionResult ShowStudent()
{
// Create a new student
Student s = new Student();
s.Name = "Rahul";
s.Age = 12;
s.Class = "6th";
📌 This code makes a Student object and sends it to the view using View(s).
html
CopyEdit
@model MyStudentApp.Models.Student
<h2>Student Information</h2>
<p><strong>Name:</strong> @Model.Name</p>
<p><strong>Age:</strong> @Model.Age</p>
<p><strong>Class:</strong> @Model.Class</p>
🎉 This view will now show the student info on the screen.
arduino
CopyEdit
http://localhost:1234/Student/ShowStudent
(Your number might be different, just copy from the address bar)
✅ Final Output:
You will see:
makefile
CopyEdit
Student Information
Name: Rahul
Age: 12
Class: 6th
=====================================================================================
Write a program for student registration with Crud Operations
csharp
CopyEdit
namespace StudentRegistrationApp.Models
{
public class Student
{
public int Id { get; set; } // Unique ID for each student
public string Name { get; set; } // Student name
public int Age { get; set; } // Student age
public string Class { get; set; } // Student class
}
}
🧠 Model: This is like a student’s information card that holds the student’s details!
🧩 Step 3: Create a Database Context (The helper robot)
1. Right-click on Models folder ➜ Add ➜ Class
csharp
CopyEdit
using System.Data.Entity;
namespace StudentRegistrationApp.Models
{
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
}
This allows you to talk to the database and store/retrieve student data! 🧑💻
Now, Visual Studio has generated all the CRUD actions and views for us! 🎉
● This is where the user will enter student details (name, age, class).
● Open Views/Student/Create.cshtml and make sure it has this code:
html
CopyEdit
@model StudentRegistrationApp.Models.Student
<h2>Create Student</h2>
@using (Html.BeginForm())
{
<div>
<label for="Name">Name:</label>
@Html.TextBoxFor(m => m.Name)
</div>
<div>
<label for="Age">Age:</label>
@Html.TextBoxFor(m => m.Age)
</div>
<div>
<label for="Class">Class:</label>
@Html.TextBoxFor(m => m.Class)
</div>
<div>
<button type="submit">Create</button>
</div>
}
This view is where you can add a new student by filling out their information.
html
CopyEdit
@model IEnumerable<StudentRegistrationApp.Models.Student>
<h2>Student List</h2>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Class</th>
<th>Actions</th>
</tr>
@foreach (var student in Model)
{
<tr>
<td>@student.Name</td>
<td>@student.Age</td>
<td>@student.Class</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = student.Id })
@Html.ActionLink("Delete", "Delete", new { id = student.Id })
</td>
</tr>
}
</table>
This view shows a table of all students and gives options to edit or delete them.
arduino
CopyEdit
http://localhost:1234/Student/Index
● View: You should see the student’s name, age, and class on the list.
=====================================================================================
Create a program for Button Group, and write a class for a basic
Button Group
Now you've created a new Windows Forms application where we can design our Button Group! 🎉
2. Drag and drop 3 buttons from the Toolbox onto the form. They should be named button1, button2,
and button3.
Now, let’s give each button a label so that we can easily identify them:
You can change the Text property of the buttons to these labels.
csharp
CopyEdit
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ButtonGroupApp
{
public class ButtonGroup
{
private List<Button> buttons; // List to store buttons
private Button selectedButton; // The currently selected button
public ButtonGroup()
{
buttons = new List<Button>(); // Initialize the list
selectedButton = null; // No button is selected at the start
}
● AddButton(Button button): Adds buttons to the Button Group and adds a click event to each button.
● SelectButton(Button button): When a button is clicked, it becomes the selected button, and the
others are deselected.
1. Open Form1.cs and add the following code at the top to use the ButtonGroup class:
csharp
CopyEdit
using ButtonGroupApp; // Import the ButtonGroup class
2. In the Form1 class, write the following code to set up the buttons and the button group:
csharp
CopyEdit
public partial class Form1 : Form
{
private ButtonGroup buttonGroup;
public Form1()
{
InitializeComponent();
buttonGroup = new ButtonGroup(); // Create a new ButtonGroup
● Now, when you click on any button, only one will be highlighted at a time.
3. When you click on any button, it will highlight that button, and the others will deselect.
=====================================================================================
Design a Simple one-page template for photo galleries, portfolios,
and more.
4. Name the project PhotoGalleryMVC (or any name you like).
This will create a basic MVC project for you with all the necessary files!
Now, open the Photo.cs file and add the following code:
csharp
CopyEdit
using System;
namespace PhotoGalleryMVC.Models
{
public class Photo
{
public int Id { get; set; }
public string Title { get; set; }
public string ImagePath { get; set; }
}
}
🧠 What This Code Does:
● Id: A unique identifier for each photo.
Now, open the PhotoController.cs file and update it with the following code:
csharp
CopyEdit
using Microsoft.AspNetCore.Mvc;
using PhotoGalleryMVC.Models;
using System.Collections.Generic;
namespace PhotoGalleryMVC.Controllers
{
public class PhotoController : Controller
{
public IActionResult Index()
{
// Sample list of photos
var photos = new List<Photo>
{
new Photo { Id = 1, Title = "Photo 1", ImagePath =
"/images/photo1.jpg" },
new Photo { Id = 2, Title = "Photo 2", ImagePath =
"/images/photo2.jpg" },
new Photo { Id = 3, Title = "Photo 3", ImagePath =
"/images/photo3.jpg" }
};
return View(photos);
}
}
}
🧠 What This Code Does:
● The Index() method sends a list of photos to the View.
● We create a List of Photo objects and add some sample data (photo title and image path).
3. Name the view Index.cshtml (this is the default view for the Index action in PhotoController).
Now, open the Index.cshtml file and add the following code:
html
CopyEdit
@model IEnumerable<PhotoGalleryMVC.Models.Photo>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Photo Gallery</title>
<link rel="stylesheet" href="~/css/style.css"> <!-- Link to CSS file -->
</head>
<body>
<header>
<h1>My Photo Gallery</h1>
</header>
<section class="gallery">
@foreach (var photo in Model)
{
<div class="photo">
<img src="@photo.ImagePath" alt="@photo.Title">
<h2>@photo.Title</h2>
</div>
}
</section>
</body>
</html>
● The foreach loop goes through each photo in the list and displays the image and title.
● @photo.ImagePath and @photo.Title are used to display the image and title of each photo.
2. Inside the css folder, create a new file named style.css.
css
CopyEdit
/* General Styling */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
text-align: center;
}
/* Gallery Section */
.gallery {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 20px;
}
/* Photo Styling */
.photo {
margin: 10px;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.photo img {
width: 300px;
height: 200px;
object-fit: cover;
border-radius: 10px;
transition: transform 0.3s ease;
}
.photo img:hover {
transform: scale(1.1);
}
h1 {
color: #333;
font-size: 2.5rem;
margin-top: 20px;
}
● Hover effect: When you hover over an image, it grows slightly to make it more interactive.
● Box shadows and rounded corners make the photos look neat and polished.
2. Add some images (like photo1.jpg, photo2.jpg, etc.) into the images folder.
Make sure the image paths in PhotoController.cs match the actual filenames of your images.
2. Your browser should open, showing the photo gallery with your images!
=====================================================================================