dotnet-lab3-MVC
dotnet-lab3-MVC
Lab Assignment-3
Net Centric Computing (CSC 367)
Prepared By:
Suman Khadka
B.Sc. CSIT 076 Batch, 6th Semester
Roll no: 54 (section-B)
Submitted To:
Ramesh Kharbuja
Lecturer, Net Centric Computing
CSIT Department
ASP.NET MVC
ASP.NET MVC is a web application framework developed by Microsoft that follows the Model-
View-Controller (MVC) architectural pattern. It is a part of the ASP.NET framework and is used
for building scalable and maintainable web applications.
In the MVC pattern, the model represents the data and business logic, the view represents the
user interface, and the controller manages the communication between the model and the view.
ASP.NET MVC follows this pattern and provides developers with tools and features to build
web applications in a modular and maintainable way.
Some of the key features of ASP.NET MVC include:
1. Routing: It allows developers to define URL patterns for the application's pages and
actions.
2. Razor View Engine: A view engine that enables the creation of dynamic and reusable
views with HTML markup and C# code.
3. Action Filters: Enables developers to apply pre- or post-processing logic to controller
actions.
4. Dependency Injection: It allows for the easy integration of third-party libraries and
frameworks.
5. Testing: ASP.NET MVC provides built-in support for unit testing and integration testing.
In MVC, there are three main parts: the Model, the View, and the Controller. Here's what each
part does:
• Model: This is where the data and business logic of the application are stored. Think of it as
the "brain" of the application. It's responsible for managing and processing data.
• View: This is what the user sees on their screen. It's responsible for displaying the data from
the Model in a way that makes sense to the user.
• Controller: This is what ties the Model and the View together. It receives input from the
user, uses the Model to process that input, and then sends the results to the View to be
displayed.
➢ Action:
an Action in .NET MVC is a method defined in a Controller class that handles a specific
HTTP request from the user and returns an appropriate response. The Action is
responsible for processing the user's input, manipulating the data if necessary, and
selecting an appropriate View to be displayed to the user. Actions are an essential part of
the MVC architecture in .NET, and they allow developers to build dynamic, responsive
web applications.
Example:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to my website!";
return View();
}
}
In .NET, the Model is typically implemented using classes that define the structure of the data
and any business rules that apply to that data. The View is typically implemented using a
templating language called Razor, which allows developers to mix HTML and C# code to
dynamically generate web pages. The Controller is implemented using C# classes that handle
incoming HTTP requests, manipulate the Model as necessary, and select the appropriate View to
display the response.
The ASP.NET MVC framework provides a set of tools and features to help developers build web
applications using the MVC pattern. This includes a routing engine that allows developers to
map URLs to specific Controller actions, and a powerful templating engine (Razor) that makes it
easy to create dynamic Views.
Overall, MVC in .NET is a way to organize the code of a web application into separate parts,
each with its own responsibility. This can make it easier to build and maintain large web
applications over time.
Program
//HomeController.cs
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using webMVC.Models;
namespace webMVC.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
//Index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web
apps with ASP.NET Core</a>.</p>
</div>
//Privacy.cshtml
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>
//Suman.cshtml
@{
ViewData["Title"] = "Suman";
}
<h1>Suman Khadka</h1>
<p>Welcome to the page, Suman.</p>
<p>New action Suman and view is added.</p>
Output
Simple MVC project with the default templates.
Action “Home”
Action “Privacy”
Adding new controller “BMC” in controller folder with several actions, views.
Styling is done in style.css which is included inside wwwroot because it is static.
Various return types are illustrated with the help of actions.
//BmcController.cs
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using webMVC.Models;
namespace webMVC.Controllers
{
public class BmcController : Controller
{
public IActionResult Bmc()
{
return View();
}
<h1>Bmc</h1>
<h2>Welcome to the Bhaktapur Multiple Campus homepage</h2>
//Contact.cshtml
@{
ViewData["Title"] = "Contact";
}
<h1>Contact</h1>
<p>Contact number: 01-6610200</p>
Output
Adding BmcController to Controllers and adding two actions Bmc() and Contact()
Outputs for Actions return types:
Content type
Json type
Partial type
File type
namespace webMVC.Controllers
{
public class StudentController : Controller
{
public IActionResult Index()
{
return View();
}
public ActionResult StudentView()
{
StudentModel suman = new StudentModel();
suman.FirstName = "Suman";
suman.LastName = "Khadka";
suman.Age = 20;
suman.Address = "Kadaghari";
suman.CollegeName= "BMC";
return View(suman);
}
return View(Students);
}
}
}
Now, adding view following is formed.
This is customized model.
//StudentView.cshtml
@model StudentModel
@{
ViewData["Title"] = "StudentView";
}
<h1>StudentView</h1>
<div>
<h4>StudentModel</h4>
<hr/>
</div>
<div>
<label>Name: </label>
<label>@Model.FirstName</label>
<label>@Model.LastName</label><br />
<label>Age: </label>
<label>@Model.Age</label><br />
<label>Address</label>
<label>@Model.Address</label><br />
<label>College Name: </label>
<label>@Model.CollegeName</label><br />
</div>
@{
ViewData["Title"] = "StudentsView";
}
<h1>StudentsView</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.CollegeName)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.CollegeName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey
*/ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */
})
</td>
</tr>
}
</tbody>
</table>