aspnetcore-lab 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aspnetcore_lab-0.1.0/PKG-INFO +53 -0
- aspnetcore_lab-0.1.0/README.md +46 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/__init__.py +3 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/__main__.py +4 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/01_console_app_razor_page.txt +81 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/02_mvc_web_app.txt +75 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/03_razor_views.txt +109 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/04_strongly_typed_view.txt +161 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/05_06_form_handling_validation.txt +127 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/07_partial_views.txt +95 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/08_session_data.txt +168 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/09_1_service_application.txt +85 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/09_2_exception_logging.txt +285 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/10_entity_framework_core.txt +128 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/practicals/11_validation_assignment.txt +178 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab/viewer.py +108 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab.egg-info/PKG-INFO +53 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab.egg-info/SOURCES.txt +21 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab.egg-info/dependency_links.txt +1 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab.egg-info/entry_points.txt +2 -0
- aspnetcore_lab-0.1.0/aspnetcore_lab.egg-info/top_level.txt +1 -0
- aspnetcore_lab-0.1.0/pyproject.toml +19 -0
- aspnetcore_lab-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aspnetcore-lab
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Numbered .NET (ASP.NET Core MVC) practical steps and code viewer
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
|
|
8
|
+
# aspnetcore-lab
|
|
9
|
+
|
|
10
|
+
Numbered viewer for the .NET practicals (steps + code converted to text).
|
|
11
|
+
Type a practical number to display it in **red**; type `0` to exit.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
```
|
|
15
|
+
python -m pip install aspnetcore-lab
|
|
16
|
+
```
|
|
17
|
+
(or, from this folder: `python -m pip install .`)
|
|
18
|
+
|
|
19
|
+
## Run in IDLE
|
|
20
|
+
```python
|
|
21
|
+
import runpy
|
|
22
|
+
runpy.run_module("aspnetcore_lab", run_name="__main__")
|
|
23
|
+
```
|
|
24
|
+
or
|
|
25
|
+
```python
|
|
26
|
+
import aspnetcore_lab
|
|
27
|
+
aspnetcore_lab.main()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Run in a terminal / command prompt
|
|
31
|
+
```
|
|
32
|
+
python -m aspnetcore_lab
|
|
33
|
+
aspnetcore-lab
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Menu
|
|
37
|
+
| Number | Practical |
|
|
38
|
+
|--------|-----------|
|
|
39
|
+
| 1 | Console app (calculator) and Razor Page |
|
|
40
|
+
| 2 | Creation of web apps in MVC |
|
|
41
|
+
| 3 | Working with Razor views |
|
|
42
|
+
| 4 | Strongly typed view |
|
|
43
|
+
| 5 / 6 | Form handling and model binding, client + server validation |
|
|
44
|
+
| 7 | Partial views |
|
|
45
|
+
| 8 | Session data usage |
|
|
46
|
+
| 9.1 | Service application (dependency injection) |
|
|
47
|
+
| 9.2 | Exception handling and logging |
|
|
48
|
+
| 10 | Entity Framework Core setup, migration and CRUD |
|
|
49
|
+
| 11 | Validation assignment |
|
|
50
|
+
|
|
51
|
+
Type `m` to show the menu again, `9` to show both 9.1 and 9.2.
|
|
52
|
+
|
|
53
|
+
This package only displays practical text; it does not execute the practical code.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# aspnetcore-lab
|
|
2
|
+
|
|
3
|
+
Numbered viewer for the .NET practicals (steps + code converted to text).
|
|
4
|
+
Type a practical number to display it in **red**; type `0` to exit.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
```
|
|
8
|
+
python -m pip install aspnetcore-lab
|
|
9
|
+
```
|
|
10
|
+
(or, from this folder: `python -m pip install .`)
|
|
11
|
+
|
|
12
|
+
## Run in IDLE
|
|
13
|
+
```python
|
|
14
|
+
import runpy
|
|
15
|
+
runpy.run_module("aspnetcore_lab", run_name="__main__")
|
|
16
|
+
```
|
|
17
|
+
or
|
|
18
|
+
```python
|
|
19
|
+
import aspnetcore_lab
|
|
20
|
+
aspnetcore_lab.main()
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Run in a terminal / command prompt
|
|
24
|
+
```
|
|
25
|
+
python -m aspnetcore_lab
|
|
26
|
+
aspnetcore-lab
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Menu
|
|
30
|
+
| Number | Practical |
|
|
31
|
+
|--------|-----------|
|
|
32
|
+
| 1 | Console app (calculator) and Razor Page |
|
|
33
|
+
| 2 | Creation of web apps in MVC |
|
|
34
|
+
| 3 | Working with Razor views |
|
|
35
|
+
| 4 | Strongly typed view |
|
|
36
|
+
| 5 / 6 | Form handling and model binding, client + server validation |
|
|
37
|
+
| 7 | Partial views |
|
|
38
|
+
| 8 | Session data usage |
|
|
39
|
+
| 9.1 | Service application (dependency injection) |
|
|
40
|
+
| 9.2 | Exception handling and logging |
|
|
41
|
+
| 10 | Entity Framework Core setup, migration and CRUD |
|
|
42
|
+
| 11 | Validation assignment |
|
|
43
|
+
|
|
44
|
+
Type `m` to show the menu again, `9` to show both 9.1 and 9.2.
|
|
45
|
+
|
|
46
|
+
This package only displays practical text; it does not execute the practical code.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
PRACTICAL 1: CONSOLE APPLICATION (CALCULATOR) AND RAZOR PAGE
|
|
2
|
+
|
|
3
|
+
A. CONSOLE APPLICATION - SIMPLE CALCULATOR
|
|
4
|
+
|
|
5
|
+
STEPS:
|
|
6
|
+
1. Open Visual Studio and click "Create a new project".
|
|
7
|
+
2. Select "Console App" and click Next.
|
|
8
|
+
3. Give the project name and directory (location), then click Create.
|
|
9
|
+
4. Write the code given below in Program.cs.
|
|
10
|
+
5. Press F5 (or Ctrl+F5) to run the program and enter the values.
|
|
11
|
+
|
|
12
|
+
CODE: (Program.cs)
|
|
13
|
+
using System;
|
|
14
|
+
|
|
15
|
+
namespace p1_evenodd
|
|
16
|
+
{
|
|
17
|
+
class Program
|
|
18
|
+
{
|
|
19
|
+
static void Main(string[] args)
|
|
20
|
+
{
|
|
21
|
+
double a = 0;
|
|
22
|
+
double b = 0;
|
|
23
|
+
Console.WriteLine("enter num1");
|
|
24
|
+
a = double.Parse(Console.ReadLine());
|
|
25
|
+
Console.WriteLine("enter num2");
|
|
26
|
+
b = double.Parse(Console.ReadLine());
|
|
27
|
+
Console.WriteLine("select operator(+,-,*,/)");
|
|
28
|
+
Console.WriteLine("\ta.Add");
|
|
29
|
+
Console.WriteLine("\tb.Sub");
|
|
30
|
+
Console.WriteLine("\tc.Mul");
|
|
31
|
+
Console.WriteLine("\td.Div");
|
|
32
|
+
string op = Console.ReadLine().ToLower();
|
|
33
|
+
switch (op)
|
|
34
|
+
{
|
|
35
|
+
case "a":
|
|
36
|
+
Console.WriteLine(a + b);
|
|
37
|
+
break;
|
|
38
|
+
case "b":
|
|
39
|
+
Console.WriteLine(a - b);
|
|
40
|
+
break;
|
|
41
|
+
case "c":
|
|
42
|
+
Console.WriteLine(a * b);
|
|
43
|
+
break;
|
|
44
|
+
case "d":
|
|
45
|
+
Console.WriteLine(a / b);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
OUTPUT:
|
|
53
|
+
enter num1
|
|
54
|
+
5
|
|
55
|
+
enter num2
|
|
56
|
+
2
|
|
57
|
+
select operator(+,-,*,/)
|
|
58
|
+
a.Add
|
|
59
|
+
b.Sub
|
|
60
|
+
c.Mul
|
|
61
|
+
d.Div
|
|
62
|
+
d
|
|
63
|
+
2.5
|
|
64
|
+
|
|
65
|
+
(a -> 7, c -> 10, d -> 2.5 for the inputs 5 and 2)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
B. RAZOR PAGE
|
|
69
|
+
|
|
70
|
+
STEPS:
|
|
71
|
+
1. Create a new project and select "ASP.NET Core Web Application", then click Next.
|
|
72
|
+
2. Give the project name and click Create.
|
|
73
|
+
3. Select the "Empty" template and click Create.
|
|
74
|
+
4. In Solution Explorer, right click on the project -> Add -> New Item.
|
|
75
|
+
5. Select "Razor Page" and click Add (e.g. Index.cshtml).
|
|
76
|
+
|
|
77
|
+
Index.cshtml generated by the Razor Page template:
|
|
78
|
+
@page
|
|
79
|
+
@model WebApplication1.IndexModel
|
|
80
|
+
@{
|
|
81
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
PRACTICAL 2: CREATION OF WEB APPS IN MVC
|
|
2
|
+
|
|
3
|
+
STEPS:
|
|
4
|
+
1. File > New > Project > ASP.NET Core Web Application > select MVC template.
|
|
5
|
+
2. In Models > Add > New Item > Class > product.cs and write the model code.
|
|
6
|
+
3. In Controllers > HomeController.cs > write the code to create the product list.
|
|
7
|
+
4. In Views > Home > Index.cshtml > write the code to show the product list.
|
|
8
|
+
5. In wwwroot > Add > New Folder "images" > add the product images
|
|
9
|
+
(laptop.jpg, mousee.jpg, cpu.jpg, keyboard.jpg, moniter.jpg).
|
|
10
|
+
6. Build > Build Solution (no errors) > Run.
|
|
11
|
+
|
|
12
|
+
CODE: (Models/product.cs)
|
|
13
|
+
namespace WebApplication1.Models
|
|
14
|
+
{
|
|
15
|
+
public class product
|
|
16
|
+
{
|
|
17
|
+
public int id { get; set; }
|
|
18
|
+
public string Name { get; set; }
|
|
19
|
+
public Decimal Price { get; set; }
|
|
20
|
+
public string ImagePath { get; set; }
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
CODE: (Controllers/HomeController.cs)
|
|
25
|
+
using System.Collections.Generic;
|
|
26
|
+
using WebApplication1.Models;
|
|
27
|
+
|
|
28
|
+
namespace WebApplication1.Controllers
|
|
29
|
+
{
|
|
30
|
+
public class HomeController : Controller
|
|
31
|
+
{
|
|
32
|
+
private readonly ILogger<HomeController> _logger;
|
|
33
|
+
|
|
34
|
+
public HomeController(ILogger<HomeController> logger)
|
|
35
|
+
{
|
|
36
|
+
_logger = logger;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public IActionResult Index()
|
|
40
|
+
{
|
|
41
|
+
var products = new List<product>
|
|
42
|
+
{
|
|
43
|
+
new product{id=1,Name="Laptop",Price=35000,ImagePath="/images/laptop.jpg"},
|
|
44
|
+
new product{id=2,Name="Mouse",Price=300,ImagePath="/images/mousee.jpg"},
|
|
45
|
+
new product{id=3,Name="C.P.U",Price=15000,ImagePath="/images/cpu.jpg"},
|
|
46
|
+
new product{id=4,Name="Keyboard",Price=3000,ImagePath="/images/keyboard.jpg"},
|
|
47
|
+
new product{id=5,Name="Monitor",Price=20000,ImagePath="/images/moniter.jpg"}
|
|
48
|
+
};
|
|
49
|
+
return View(products);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public IActionResult Privacy()
|
|
53
|
+
{
|
|
54
|
+
return View();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
CODE: (Views/Home/Index.cshtml)
|
|
60
|
+
@model IEnumerable<WebApplication1.Models.product>
|
|
61
|
+
<h2>Product List</h2>
|
|
62
|
+
<ul>
|
|
63
|
+
@foreach (var product in Model)
|
|
64
|
+
{
|
|
65
|
+
<li>
|
|
66
|
+
<img src="@product.ImagePath"
|
|
67
|
+
alt="@product.Name"
|
|
68
|
+
style="width:150px;height:auto" />
|
|
69
|
+
@product.Price
|
|
70
|
+
</li>
|
|
71
|
+
}
|
|
72
|
+
</ul>
|
|
73
|
+
|
|
74
|
+
OUTPUT:
|
|
75
|
+
The browser shows "Product List" with each product image (150px wide) and its price.
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
PRACTICAL 3: WORKING WITH RAZOR VIEWS
|
|
2
|
+
|
|
3
|
+
STEPS:
|
|
4
|
+
1. Create a new project, same as the MVC project (ASP.NET Core Web Application > MVC).
|
|
5
|
+
2. For creating the view page: Views > Home > Add > View > Price.cshtml.
|
|
6
|
+
3. In Controllers > HomeController, add the IActionResult "Price" to the code.
|
|
7
|
+
4. In Index.cshtml, add a link for Price.cshtml.
|
|
8
|
+
5. Run the project and click the link.
|
|
9
|
+
|
|
10
|
+
A. IF / ELSE IN RAZOR VIEW
|
|
11
|
+
|
|
12
|
+
CODE: (Views/Home/Price.cshtml)
|
|
13
|
+
@{
|
|
14
|
+
var price = 60;
|
|
15
|
+
}
|
|
16
|
+
<html>
|
|
17
|
+
<body>
|
|
18
|
+
@if (price > 50)
|
|
19
|
+
{
|
|
20
|
+
<p>The price is greater than 50.</p>
|
|
21
|
+
}
|
|
22
|
+
else
|
|
23
|
+
{
|
|
24
|
+
<p>Price is less than 50.</p>
|
|
25
|
+
}
|
|
26
|
+
</body>
|
|
27
|
+
</html>
|
|
28
|
+
|
|
29
|
+
CODE: (Controllers/HomeController.cs - add this action)
|
|
30
|
+
public IActionResult Price()
|
|
31
|
+
{
|
|
32
|
+
return View();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
CODE: (Views/Home/Index.cshtml)
|
|
36
|
+
@{
|
|
37
|
+
ViewData["Title"] = "Home Page";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
<div class="text-center">
|
|
41
|
+
<h1 class="display-4">Welcome</h1>
|
|
42
|
+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
|
|
43
|
+
<p><a href="Home/Price"> Razor Example </a></p>
|
|
44
|
+
</div>
|
|
45
|
+
|
|
46
|
+
OUTPUT:
|
|
47
|
+
Click "Razor Example" -> the page shows: The price is greater than 50.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
B. SWITCH CASE IN RAZOR VIEW
|
|
51
|
+
|
|
52
|
+
STEPS:
|
|
53
|
+
1. In HomeController.cs add the action Month.
|
|
54
|
+
2. In Views > Home add the view Month.cshtml.
|
|
55
|
+
3. In Index.cshtml add a link for Home/Month.
|
|
56
|
+
|
|
57
|
+
CODE: (Controllers/HomeController.cs - add this action)
|
|
58
|
+
public IActionResult Month()
|
|
59
|
+
{
|
|
60
|
+
return View();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
CODE: (Views/Home/Month.cshtml)
|
|
64
|
+
@{
|
|
65
|
+
ViewData["Title"] = "Month";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@{
|
|
69
|
+
var month = DateTime.Now.ToString("MMMM");
|
|
70
|
+
var message = "";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
<html>
|
|
74
|
+
<body>
|
|
75
|
+
@{
|
|
76
|
+
switch (month)
|
|
77
|
+
{
|
|
78
|
+
case "January":
|
|
79
|
+
message = "Starting of the Year";
|
|
80
|
+
break;
|
|
81
|
+
case "July":
|
|
82
|
+
message = "Middle of the Year";
|
|
83
|
+
break;
|
|
84
|
+
case "December":
|
|
85
|
+
message = "Ending of the Year";
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
message = "Any Month in between";
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
<p>@message</p>
|
|
93
|
+
</body>
|
|
94
|
+
</html>
|
|
95
|
+
|
|
96
|
+
CODE: (Views/Home/Index.cshtml)
|
|
97
|
+
@{
|
|
98
|
+
ViewData["Title"] = "Home Page";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
<div class="text-center">
|
|
102
|
+
<h1 class="display-4">Welcome</h1>
|
|
103
|
+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
|
|
104
|
+
<p><a href="Home/Month">CLick</a></p>
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
OUTPUT:
|
|
108
|
+
Click "CLick" -> the page shows the message for the current month
|
|
109
|
+
(January / July / December / "Any Month in between").
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
PRACTICAL 4: STRONGLY TYPED VIEW
|
|
2
|
+
|
|
3
|
+
STEPS:
|
|
4
|
+
1. Create new project > select ASP.NET Web Application > select MVC > project created.
|
|
5
|
+
2. Models > Add > New Item > Class > new model class file (Student.cs) created > write code.
|
|
6
|
+
3. Go in Controllers > Add > Controller > "MVC Controller - Empty" > StudentController > write code.
|
|
7
|
+
4. For creating a strongly typed view: in Views create a new folder "Student".
|
|
8
|
+
5. In the folder Student create two views: "Index" and "Create" > write code.
|
|
9
|
+
6. Run the project and open /Student.
|
|
10
|
+
|
|
11
|
+
CODE: (Models/Student.cs)
|
|
12
|
+
namespace Prac4.Models
|
|
13
|
+
{
|
|
14
|
+
public class Student
|
|
15
|
+
{
|
|
16
|
+
public int Id { get; set; }
|
|
17
|
+
public string FullName { get; set; }
|
|
18
|
+
public int Age { get; set; }
|
|
19
|
+
public string Course { get; set; }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
CODE: (Controllers/StudentController.cs)
|
|
24
|
+
using System;
|
|
25
|
+
using System.Collections.Generic;
|
|
26
|
+
using System.Linq;
|
|
27
|
+
using System.Threading.Tasks;
|
|
28
|
+
using Microsoft.AspNetCore.Mvc;
|
|
29
|
+
using Prac4.Models;
|
|
30
|
+
|
|
31
|
+
namespace Prac4.Controllers
|
|
32
|
+
{
|
|
33
|
+
public class StudentController : Controller
|
|
34
|
+
{
|
|
35
|
+
private static List<Student> students = new List<Student>
|
|
36
|
+
{
|
|
37
|
+
new Student{Id=1,FullName="Alice Johnson",Age=21,Course="Computer Science"},
|
|
38
|
+
new Student{Id=2,FullName="Bob Smith",Age=20,Course="Electrical Engineering"}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
public IActionResult Index()
|
|
42
|
+
{
|
|
43
|
+
return View(students);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public IActionResult Create()
|
|
47
|
+
{
|
|
48
|
+
return View();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
[HttpPost]
|
|
52
|
+
public IActionResult Create(Student student)
|
|
53
|
+
{
|
|
54
|
+
if (ModelState.IsValid)
|
|
55
|
+
{
|
|
56
|
+
student.Id = students.Count + 1;
|
|
57
|
+
students.Add(student);
|
|
58
|
+
return RedirectToAction("Index");
|
|
59
|
+
}
|
|
60
|
+
return View(student);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
CODE: (Views/Student/Index.cshtml)
|
|
66
|
+
@model IEnumerable<Prac4.Models.Student>
|
|
67
|
+
|
|
68
|
+
<h2>Student List</h2>
|
|
69
|
+
<table class="table">
|
|
70
|
+
<thead>
|
|
71
|
+
<tr>
|
|
72
|
+
<th>Name</th>
|
|
73
|
+
<th>Age</th>
|
|
74
|
+
<th>Course</th>
|
|
75
|
+
</tr>
|
|
76
|
+
</thead>
|
|
77
|
+
<tbody>
|
|
78
|
+
@foreach (var student in Model)
|
|
79
|
+
{
|
|
80
|
+
<tr>
|
|
81
|
+
<td>@student.FullName</td>
|
|
82
|
+
<td>@student.Age</td>
|
|
83
|
+
<td>@student.Course</td>
|
|
84
|
+
</tr>
|
|
85
|
+
}
|
|
86
|
+
</tbody>
|
|
87
|
+
</table>
|
|
88
|
+
|
|
89
|
+
CODE: (Views/Student/Create.cshtml)
|
|
90
|
+
@model Prac4.Models.Student
|
|
91
|
+
<h2>Add New Student</h2>
|
|
92
|
+
<form asp-action="Create" method="post">
|
|
93
|
+
<div>
|
|
94
|
+
<label asp-for="FullName"></label>
|
|
95
|
+
<input asp-for="FullName"/>
|
|
96
|
+
</div>
|
|
97
|
+
<div>
|
|
98
|
+
<label asp-for="Age"></label>
|
|
99
|
+
<input asp-for="Age" type="number" />
|
|
100
|
+
</div>
|
|
101
|
+
<div>
|
|
102
|
+
<label asp-for="Course"></label>
|
|
103
|
+
<input asp-for="Course" />
|
|
104
|
+
</div>
|
|
105
|
+
<button type="submit">Save</button>
|
|
106
|
+
</form>
|
|
107
|
+
|
|
108
|
+
OUTPUT:
|
|
109
|
+
/Student shows the Student List table; the Create page adds a new student to the list.
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
PRODUCT EXAMPLE (same pattern: Product model + ProductController + Index/Create views)
|
|
113
|
+
|
|
114
|
+
Model fields used: Id, Name, Category, Price (Models/Product.cs)
|
|
115
|
+
|
|
116
|
+
CODE: (Controllers/ProductController.cs)
|
|
117
|
+
using System;
|
|
118
|
+
using System.Collections.Generic;
|
|
119
|
+
using System.Linq;
|
|
120
|
+
using System.Threading.Tasks;
|
|
121
|
+
using Microsoft.AspNetCore.Mvc;
|
|
122
|
+
using Prac4.Models;
|
|
123
|
+
|
|
124
|
+
namespace Prac4.Controllers
|
|
125
|
+
{
|
|
126
|
+
public class ProductController : Controller
|
|
127
|
+
{
|
|
128
|
+
private static List<Product> products = new List<Product>
|
|
129
|
+
{
|
|
130
|
+
new Product{Id=1,Name="Notebook",Category="Stationery",Price=70},
|
|
131
|
+
new Product{Id=2,Name="Mouse",Category="Electronic",Price=200},
|
|
132
|
+
new Product{Id=3,Name="Jeans",Category="Clothes",Price=1000}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
public IActionResult Index()
|
|
136
|
+
{
|
|
137
|
+
return View(products);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public IActionResult Create()
|
|
141
|
+
{
|
|
142
|
+
return View();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
[HttpPost]
|
|
146
|
+
public IActionResult Create(Product product)
|
|
147
|
+
{
|
|
148
|
+
if (ModelState.IsValid)
|
|
149
|
+
{
|
|
150
|
+
product.Id = products.Count + 1;
|
|
151
|
+
products.Add(product);
|
|
152
|
+
return RedirectToAction("Index");
|
|
153
|
+
}
|
|
154
|
+
return View(product);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
OUTPUT:
|
|
160
|
+
Product List table with columns ID, Name, Category, Price
|
|
161
|
+
(1 Notebook Stationery 70 / 2 Mouse Electronic 200 / 3 Jeans Clothes 1000).
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
PRACTICAL 5 & 6: FORM HANDLING AND MODEL BINDING WITH CLIENT SIDE AND SERVER SIDE VALIDATION
|
|
2
|
+
|
|
3
|
+
Form Handling : captures user input from a form.
|
|
4
|
+
Model Binding : maps form data to C# objects automatically.
|
|
5
|
+
|
|
6
|
+
STEPS:
|
|
7
|
+
1. Create ASP.NET Core Web App (MVC) project.
|
|
8
|
+
2. Create a new controller in the Controllers folder named ContactController.
|
|
9
|
+
3. Create a class file in the Models folder named ContactForm.cs.
|
|
10
|
+
4. In the Views folder, create a new folder named Contact.
|
|
11
|
+
5. Inside Views/Contact, add Index.cshtml for the form view.
|
|
12
|
+
6. Inside Views/Contact, add Thanks.cshtml for the confirmation page.
|
|
13
|
+
7. In Startup.cs / Program.cs, (optional) set the default route to controller "Contact".
|
|
14
|
+
|
|
15
|
+
SERVER SIDE:
|
|
16
|
+
|
|
17
|
+
CODE: (Controllers/ContactController.cs)
|
|
18
|
+
using System;
|
|
19
|
+
using System.Collections.Generic;
|
|
20
|
+
using System.Linq;
|
|
21
|
+
using System.Threading.Tasks;
|
|
22
|
+
using Microsoft.AspNetCore.Mvc;
|
|
23
|
+
using Prac5.Models;
|
|
24
|
+
|
|
25
|
+
namespace Prac5.Controllers
|
|
26
|
+
{
|
|
27
|
+
public class ContactController : Controller
|
|
28
|
+
{
|
|
29
|
+
public IActionResult Index()
|
|
30
|
+
{
|
|
31
|
+
return View();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
[HttpPost]
|
|
35
|
+
public IActionResult Index(ContactForm form)
|
|
36
|
+
{
|
|
37
|
+
if (ModelState.IsValid)
|
|
38
|
+
{
|
|
39
|
+
return RedirectToAction("Thanks");
|
|
40
|
+
}
|
|
41
|
+
return View(form);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public IActionResult Thanks()
|
|
45
|
+
{
|
|
46
|
+
return View();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
CODE: (Views/Contact/Index.cshtml)
|
|
52
|
+
@{
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
|
|
56
|
+
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
|
|
57
|
+
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
|
|
58
|
+
@model Prac5.Models.ContactForm
|
|
59
|
+
<form asp-action="Index" method="post">
|
|
60
|
+
<div>
|
|
61
|
+
<label asp-for="Name"></label>
|
|
62
|
+
<input asp-for="Name" />
|
|
63
|
+
<span asp-validation-for="Name"></span>
|
|
64
|
+
</div>
|
|
65
|
+
<div>
|
|
66
|
+
<label asp-for="Email"></label>
|
|
67
|
+
<input asp-for="Email" />
|
|
68
|
+
<span asp-validation-for="Email"></span>
|
|
69
|
+
</div>
|
|
70
|
+
<div>
|
|
71
|
+
<label asp-for="Message"></label>
|
|
72
|
+
<textarea asp-for="Message"></textarea>
|
|
73
|
+
<span asp-validation-for="Message"></span>
|
|
74
|
+
</div>
|
|
75
|
+
<button type="submit">Send</button>
|
|
76
|
+
</form>
|
|
77
|
+
|
|
78
|
+
CODE: (Models/ContactForm.cs)
|
|
79
|
+
using System;
|
|
80
|
+
using System.Collections.Generic;
|
|
81
|
+
using System.ComponentModel.DataAnnotations;
|
|
82
|
+
using System.Linq;
|
|
83
|
+
using System.Threading.Tasks;
|
|
84
|
+
|
|
85
|
+
namespace Prac5.Models
|
|
86
|
+
{
|
|
87
|
+
public class ContactForm
|
|
88
|
+
{
|
|
89
|
+
[Required(ErrorMessage="Username is mandatory")]
|
|
90
|
+
public string Name { get; set; }
|
|
91
|
+
|
|
92
|
+
[Required(ErrorMessage = "Email is mandatory")]
|
|
93
|
+
[EmailAddress(ErrorMessage ="Invalid Email Format")]
|
|
94
|
+
public string Email { get; set; }
|
|
95
|
+
|
|
96
|
+
[StringLength(500,ErrorMessage ="Message cannot exceed 500 characters,")]
|
|
97
|
+
public string Message { get; set; }
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
CODE: (Startup.cs - Configure method: only change controller=Contact)
|
|
102
|
+
app.UseRouting();
|
|
103
|
+
|
|
104
|
+
app.UseAuthorization();
|
|
105
|
+
|
|
106
|
+
app.UseEndpoints(endpoints =>
|
|
107
|
+
{
|
|
108
|
+
endpoints.MapControllerRoute(
|
|
109
|
+
name: "default",
|
|
110
|
+
pattern: "{controller=Contact}/{action=Index}/{id?}");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
CODE: (Views/Contact/Thanks.cshtml)
|
|
114
|
+
@{
|
|
115
|
+
ViewData["Title"] = "Thanks";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
<h1>Thankyou! Your form has been submitted!</h1>
|
|
119
|
+
|
|
120
|
+
OUTPUT:
|
|
121
|
+
Leaving Name / Email empty or giving an invalid e-mail shows the error messages.
|
|
122
|
+
A valid form redirects to the Thanks page: "Thankyou! Your form has been submitted!"
|
|
123
|
+
|
|
124
|
+
CLIENT SIDE:
|
|
125
|
+
Run the output and inspect (right click > Inspect in the browser).
|
|
126
|
+
The validation messages appear without a page reload because of the jQuery validation
|
|
127
|
+
scripts (jquery.validate and jquery.validate.unobtrusive) added in Index.cshtml.
|