LIBRARY:
using System.Collections.Generic;
using System.Linq;
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
---------------------------------------
<input onclick="return confirm('Are you sure you want to delete this?')"
type="submit" class="btn btn-sm btn-danger" asp-page-handler="DeleteTransaction"
value="Delete" />
---Add JSON FILE
"ConnectionStrings": {
//một ứng dụng có thể kết nối đến nhiều CSDL khác nhau
//--> nhiều connection string khác nhau
//mỗi connection sẽ phân bieetrj với nhau thông qua te
"Q3": "server=DESKTOP-KMC6L65\\
SQLEXPRESS;database=PE_PRN_Sum21;user=LeViet;password=123"
-----------------------------------------------------------------------------------
----------------
ADD file DBContext
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
string ConnectionStr = config.GetConnectionString("Q3");
optionsBuilder.UseSqlServer(ConnectionStr);
--------------------------
ĐỔI FILE START UP
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
--------------------
URL
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context => //dấu "/"đại diện cho
đường dẫn gốc
{
await context.Response.WriteAsync("Hello World!");
});
//mô tả 1 pattern url và cách phân tích nó
endpoints.MapControllerRoute(
name: "default",
pattern:"{controller=home}/{action=index}"
);
endpoints.MapControllerRoute(
name: "pattern",
pattern: "{controller=home}/{action=index}/{id}"
);
endpoints.MapControllerRoute(
name: "pattern1",
pattern:
"{controller=home}/{action=index}/{name=SE1506}/{year=2020}"
);
});
-----------------------------------------------------------------------------------
----------------
HTML FORM
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1></h1>
<p></p>
</body>
</html>
---------------------------------------
FORM
<form action="/services/getSearch" method="post">
Room Title : <input type="text" name="room" value="@ViewBag.room"/>
<br /> <br />
Month : <input type="text" name="month" value="@ViewBag.month"/> <br
/> <br />
<input type="submit" value="Search" /> <br /> <br />
</form>
-----------------------------------------------------------------------------------
--------------
TABLE:
<table border="1" style='border-collapse:collapse'>
<tr>
<td> RoomTitle </td>
<td> FreeType </td>
<td> Month </td>
<td> Year </td>
<td> Amount </td>
<td> </td>
</tr>
@foreach(Service s in ViewBag.services)
{
<tr>
<td>@s.RoomTitle</td>
<td>@s.FeeType</td>
<td>@s.Month</td>
<td>@s.Year</td>
<td>@s.Amount</td>
@if(s.EmployeeNavigation == null)
{
<td>Unpaid</td>
}
else
{
<td>Service has been paid. Employee:
@s.EmployeeNavigation.Name</td>
}
</tr>
}
</table>
-----------------------------------
DEMO LẤY LIÊN KẾT KHÓA NGOẠI
public IActionResult Search()
{
using (var context = new PRN211_Spr22Context())
{
List<Service> services = context.Services.ToList();
foreach (Service item in services)
{
item.EmployeeNavigation = context.Employees.Where(x => x.Id ==
item.Employee).SingleOrDefault(); //
}
ViewBag.Services = services;
}
return View();
}
------------------------------------------------
DEMO LẤY DỮ LIỆU TỪ FORM
public IActionResult getSearch()
{
string roomTitle = Request.Form["room"];
string month = Request.Form["month"];
using (var context = new PRN211_Spr22Context())
{
List<Service> services = context.Services.Where(x =>
x.RoomTitle.Contains(roomTitle) && x.Month.ToString().Contains(month)).ToList();
foreach (Service item in services)
{
item.EmployeeNavigation = context.Employees.Where(x => x.Id ==
item.Employee).SingleOrDefault();
}
ViewBag.Services = services;
ViewBag.room = roomTitle;
ViewBag.month = month;
}
return View();
}
-----------------------------------------------------------------------------------
---------------
[HttpPost]
public IActionResult Search( string title, string month)
{
using (PRN211_Spr22Context context = new PRN211_Spr22Context())
{
if(title == null)
{
title = "";
}
if(month == null)
{
month = "";
}
List<Service> listSer = context.Services.Where(x =>
x.RoomTitle.Contains(title) && x.Month.ToString().Contains(month)).ToList();
foreach (Service ser in listSer)
{
ser.EmployeeNavigation = context.Employees.FirstOrDefault(x =>
x.Id == ser.Employee);
}
ViewBag.title = title;
ViewBag.month = month;
return View(listSer);
}
----------------------
DISPLAY THOONG TIN TREN 1 DONG VA CO KE VACH
<style type="text/css">
.inline {
margin: 10px;
float: left;
margin: 10px;
}
a {
font-size:medium;
}
a:active {
font-size:larger;
}
a:focus{
font-size:larger;
outline: none;
}
.vertical-row {
height: 300px;
width: 1px; /* edit this if you want */
background-color: black;
}
</style>
---------------------------------
List category:
<form asp-controller="Home" asp-action="Index" method="post" id="frm">
<select name="cateId" onchange="onSelectionChange()">
<option value="0">All Category</option>
@foreach (Category c in ViewBag.Categories)
{
@if(@ViewBag.selectedId == @c.CategoryId)
{
<option value="@c.CategoryId"selected > @c.CategoryName</option>
}else{
<option value="@c.CategoryId" > @c.CategoryName</option>
}
}
</select>
</form>
---------------------------
CSS table và padding
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
</style>