Professional Documents
Culture Documents
Chapter 3 Views in ASP - NET Core
Chapter 3 Views in ASP - NET Core
NET Core
Response
SelectAll.html
Controller
Model
DB Public class EmpController {
Public class Emp { Create.html
Emp table Id : int
SelectAll() ;
Name : String
Dept : String
Create();
Salary : double Edit();
} Delete(); Edit.html
}
HTML pages
What is View
• In ASP.NET Core MVC, Views are .cshtml files that use the C# programming
language in Razor markup.
• Usually, view files are grouped into folders named for each of the app's controllers
.
• The Home controller is represented by a Home folder inside the Views folder.
• The Home folder contains the views for the Index and Privacy, and Index
(homepage) web pages. When a user requests one of these two webpages,
controller actions in the Home controller determine which of the two views is used
to build and return a webpage to the user.
Create new View
Form2.cshtml
Form tag and HttpPost
https://localhost:00000/Emp/Form2
EmpController
1
[HttpGet]
Views Emp + public IActionResult Form2( )
{
2
return View();
Form2.cshtml
}
n1
[HttpPost] 8 5
No1 8
3 + public IActionResult Form2(int n1 , int n2 )
n2
5 {
No2
processing ();
b1 Sum 4
return View();
Result
n3 }
Form2.cshtml
Form tag and HttpPost
Views Emp https://localhost:00000/Emp/Form3
EmpController
Form3.cshtml 1
[HttpGet]
Name + public IActionResult Form3( )
{
Gender male Female 2 return View();
}
Age
[HttpPost]
+ public IActionResult Form3(string tname , string gender , int age ,
Status Active
3 string status , string notes )
{
Notes processing ();
5
4
return View();
Submit }
Form3.cshtml
Razor View (cshtml)
• 1- inline Code
• @C# code
• 2- Multiline
@{
statments
}
• 3- if statement
@if (condition)
{
}
else
{
}
Razor View (cshtml)
• 4- for statement
@for (initial ; condition , inc)
{
}
• 5- while statement
@while (condition)
{
}
Transferring data from Controllers to Views
Action Method View Scope
ViewBag ViewBag.var = data; @ViewBag.var Action View Dynamic
One View
ViewData ViewData[“Key”] = “data”; @ViewData[“key”] Action View Need Casting
One View
TempData TempData[“Key”] = “data”; @TempData[“key”] ActionActoion View Need Casting
Two Views
Session httpContext.Session.SetString( @ViewBag.var Action All Project Install
“Key”, “Data”) Register
All Pages Configure
ViewBag.var =
httpContext.Session.GetString(
“Key”);
ViewBag
https://localhost:00000/Emp/Form2
EmpController
1
[HttpGet]
Views Emp + public IActionResult Form2( )
{
2
return View();
Form2.cshtml
}
n1
[HttpPost] 8 5
No1 8
3 + public IActionResult Form2(int n1 , int n2 )
n2
5 {
No2
int sum = n1+ n2;
b1 Sum 4 ViewBag.sum = sum;
return View();
Result @ViewBag.sum
n3 }
Form2.cshtml
ViewData
https://localhost:00000/Emp/Form1
EmpController
1
[HttpGet]
Views Emp + public IActionResult Form2( )
{
2
return View();
Form2.cshtml
}
n1
[HttpPost] 8 5
No1 8
3 + public IActionResult Form2(int n1 , int n2 )
n2
5 {
No2
int sum = n1+ n2;
b1 Sum 4 ViewData[“sum”] = sum.toString();
return View();
Result @ViewData[“sum”]
n3 }
Form2.cshtml
TempData https://localhost:00000/Emp/Form4
1 EmpController
Views Emp
[HttpGet]
+ public IActionResult Form4( )
{
2 return View();
No1 5
}
[HttpPost]
6 + public IActionResult Form4(int n1 , int n2 )
No2 3
{
Sum int sum = n1+n2; 7
TempData[“sum”] = sum.ToString();
return RedirectToAction(“Form5”);
}
Form4.cshtml 4
[HttpGet]
+ public IActionResult Form5( )
5 {
No1 return View();
5
}
No2 6 [HttpPost]
[ActionName(“Form5”)]
Result @TempData[“sum”] + public IActionResult Back()
6 {
back return RedirectToAction(“Form4”);
}
Form5.cshtml
Session
• Install
• Tools Nuget Package Manager Manage Nuget Package for solution
• Browse Search for “Session”
• Install Microsoft.AspNetCore.Session
• Register
• Open Program.cs
• Builder.services.AddSession();
• Builder.services.addDistributedMemoryCache
• Configure
• App.useSession();
Session https://localhost:00000/Emp/Form6
1 EmpController
Views Emp
[HttpGet]
+ public IActionResult Form6( )
{
2 return View();
No1 5 }
[HttpPost]
6 + public IActionResult Form6(int n1 , int n2 )
No2 3 {
Sum int sum = n1+n2;
7
HttpContext.Session.SetString(“sum”, sum.ToString());
return RedirectToAction(“Form5”);
}
Form6.cshtml [HttpGet] 4
+ public IActionResult Form7( )
5 {
No1 ViewBag.sum = HttpContext.Session.GetString(“sum”);
5 return View();
}
No2 6
[HttpPost]
Result @ViewBag.sum [ActionName(“Form7”)]
6 + public IActionResult Back()
{
back return RedirectToAction(“Form4”);
}
Form7.cshtml
Cookie https://localhost:00000/Emp/Form6
1 EmpController
Views Emp
+ public IActionResult Login( )
{
var obj = Request.Cookie[“Key”];
User 2 if (obj==null)
5 return View();
else
Pass 6 return RedirectoToAction(“MyPage”);
3 }
Login
[HttpPost]
+ public IActionResult Login(String user , String pass )
{
Login.cshtml
CookieOptions obj = new CookieOptions();
5 obj.Expires = DateAndTime.Now.AddDays(7);
Response.Cookies.Append("Key", "Data", obj);
return RedirectToAction(“MyPage”);
}
[HttpGet]
6 + public IActionResult MyPage( )
{
return View();
}
MyPage.cshtml
_Layout View
_Layout.cshtml
_ViewStart.cshtml Page2.cshtml
Change the Layout for View
_Layout.cshtml
_AdminLayout.cshtml
• https://getbootstrap.com/docs/5.1/examples/
• Wwwrooot Folder
• Lib folder bootstrap distcss bootstrap.css
• css folderSite.css
• Js
• Images
Render section
• Render section tells the application that some other code coming
from view will be place here