0% found this document useful (0 votes)
32 views12 pages

Week 2 Lecture 4 Lab

This document provides an overview of ASP.Net MVC including: - Controllers find views and pass data to them - Models represent application data - Strongly typed views bind to specific model types for compile-time checking - The lecture outlines concepts like the MVC pattern, passing data from controllers to views using ViewBag, ViewData, and models, and creating models and strongly typed views.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
32 views12 pages

Week 2 Lecture 4 Lab

This document provides an overview of ASP.Net MVC including: - Controllers find views and pass data to them - Models represent application data - Strongly typed views bind to specific model types for compile-time checking - The lecture outlines concepts like the MVC pattern, passing data from controllers to views using ViewBag, ViewData, and models, and creating models and strongly typed views.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 12

Introduction to ASP.

Net MVC
Course Code: CSC 4164 Course Title: ADVANCED PROGRAMMING WITH .NET

Dept. of Computer Science


Faculty of Science and Technology

Lecture No: 04 Week No: 02 Semester: Fall 2020-21


Lecturer: Victor Stany Rozario, stany@aiub.edu
Lecture Outline

• Controller finding a view


• Passing data from Controller to View
• Models
• Strongly typed views
Controller finding a view
• The URL tells the routing mechanism which controller class to
instantiate and which action method to call and supplies the
required arguments to that method.
• The controller’s method then decides which view to use, and that
view then renders the HTML.
• The Views directory contains a folder for your controller, with the
same name as the controller.
• Within controller folder there’s a view file for each action method,
named the same as the action method.
• This is how views are associated to an action method.
Passing data from Controller to View
• ViewBag: Is a type object and a dynamic property under the
controller base class.
o It is ideally temporary data.
o It doesn’t have typecasting and null checks.
• ViewData: Is a dictionary which can contain key-value pairs where
each key must be string. Requires typecasting as well as null checks.
• Passing an object of the model class to the View.
• TempData: Stays for a subsequent HTTP Request as opposed to other
options (ViewBag and ViewData) those stay only for current request.
o Maintain data between controller actions as well as redirects.
o Typecasting and null checks required in order to avoid errors.
public ActionResult TemporaryEmployee()
{
Employee employee = new Employee
{
EmpID = “121”,
EmpFirstName = “John”,
EmpLastName = “Nguyen”
};
TempData[“Employee”] = employee;
return
RedirectToAction(“PermanentEmployee”);
}
Models
What is Model?

• Business/domain logic

• Model objects, retrieve and store model state in a persistent storage (database).
public class Customer{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public IEnumerable Orders { get; set; }
}
Creating Models
Add a folder named “Models”. In Solution Explorer, right-click the project. Select Add 
New Folder. Name the folder Models.

Right-click the Models folder and select Add  New Item. In Add New Item, select the
Class. Name the class TodoItem and click OK.

publicclassTodoItem
{
public string Key { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; }
}
Strongly typed views

• The view which binds to a specific type of View Model by passing the model
object as a parameter to the View() method is called as Strongly Typed View.

public class HomeController : Controller


{
public ActionResult Index()
{
EmployeeBusinessLayer employeeBL = new EmployeeBusinessLayer();
Employee employee = employeeBL.GetEmployeeDetails(101);
ViewBag.Header = "Employee Details";

return View(employee);
}
}
Changes in Index.cshtml View:
@model FirstMVCDemo.Models.Employee
<!DOCTYPE html>
<html> Advantages of Strongly-typed views:
<head> • Strongly Typed View in ASP.NET MVC
<meta name="viewport" provides compile-time error checking as
content="width=device-width" /> well as intelligence support.
<title>Page Title</title> • If we misspell the property name, then
</head> it comes to know at compile time rather
<body> than at runtime.
<h2>@ViewBag.Header</h2>
<table style="font-family:Arial">
<tr>
<td>Employee ID:</td>
<td>@Model.EmployeeId </td>
</tr>
<tr>
<td>Name:</td>
<td>@Model.Name</td>
</tr> </table>
</body>
</html>
Books

1. Beginning ASP.NET 4: in C# and VB; Imar Spaanjaars,2010


2. Beginning ASP.NET 3.5 in C# 2008: From Novice to Professional; 2nd
edition, Matthew MacDonald,2007
3. ASP.NET 3.5 Unleashed by Stephen Walther, 2008
4. Pro ASP.NET 3.5 in C# 2008: Includes Silverlight 2 by Matthew
MacDonald,2008
5. ASP.NET 3.5 For Dummies by Ken Cox,2008
References

1. ASP.NET; URL: https://dotnet.microsoft.com/apps/aspnet


2. URL: https://www. tutorialspoint.com/index.htm
3. URL: http://www.webdevelopmenthelp.net/2014
4. View Engines; URL: https://www.tutorialride.com/asp-net-mvc
5. URL: https://www.tutorialsteacher.com/mvc
6. Strongly typed views; URL: https://dotnettutorials.net/lesson
Thank you!

You might also like