0% found this document useful (0 votes)
29 views9 pages

Framwork

The documents contain C# code for an airline booking system. There are controllers to handle passenger and flight booking functions like inserting, updating, deleting. Models define passenger, flight, and booking data. Context class handles database operations. Views display insertion forms and output results. Controllers call context methods and pass data to views which display forms and output.

Uploaded by

Quoc Truong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views9 pages

Framwork

The documents contain C# code for an airline booking system. There are controllers to handle passenger and flight booking functions like inserting, updating, deleting. Models define passenger, flight, and booking data. Context class handles database operations. Views display insertion forms and output results. Controllers call context methods and pass data to views which display forms and output.

Uploaded by

Quoc Truong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

HanhKhachController.

cs

public class HanhKhachController : Controller


{
public IActionResult Insert()
{
return View();
}
public IActionResult Enter(HanhKhach hanhKhach)
{
int count = 1;
StoreContext context =
HttpContext.RequestServices.GetService(typeof(StoreContext)) as StoreContext;
count = context.InsertHanhKhach(hanhKhach);
if (count > 0)
ViewData["thongbao"] = "Insert thành công";
else
ViewData["thongbao"] = "Insert không thành công";
return View(hanhKhach);
}
}

ChuyenBayController.cs

public class ChuyenBayController: Controller


{
public IActionResult Index()
{
return View();
}

public IActionResult LietKe(ChuyenBay chuyenBay)


{

StoreContext context =
HttpContext.RequestServices.GetService(typeof(StoreContext)) as StoreContext;
var count1 = context.LietKeChuyenBay(chuyenBay.MaCB);
var count2 = context.ThongTinChuyenBay(chuyenBay.MaCB);
var model = new ChuyenBayViewModel
{
ListCB = count1,
ChuyenBay = count2
};

return View(model);
}

public IActionResult ThemHanhKhach(string maCB, string maHK)


{
var cus = new CT_CB
{
MaHK = maHK,
MaCB = maCB
};
int count = 1;
StoreContext context =
HttpContext.RequestServices.GetService(typeof(StoreContext)) as StoreContext;
count = context.ThemHnahKhach(cus);
if (count > 0)
ViewData["thongbao"] = "Insert thành công";
else
ViewData["thongbao"] = "Insert không thành công";
return RedirectToAction("Index");

public IActionResult Capnhat(string makh, string macb)


{
StoreContext context =
HttpContext.RequestServices.GetService(typeof(StoreContext)) as StoreContext;
CT_CB ct_cb = context.ViewCT_CB(makh,macb);

return View(ct_cb);
}
public IActionResult Update(CT_CB ct_cb)
{
int count;
StoreContext context =
HttpContext.RequestServices.GetService(typeof(StoreContext)) as StoreContext;
count = context.Update(ct_cb);
if (count > 0)
ViewData["thongbao"] = "Update thành công";
else
ViewData["thongbao"] = "Update không thành công";
return View();
}
public IActionResult Delete(string makh, string macb) {
// ViewData["id"] = Id;
StoreContext context =
HttpContext.RequestServices.GetService(typeof(StoreContext)) as StoreContext;
int count = context.Delete(makh,macb);
if (count > 0)
ViewData["thongbao"] = "Xóa thành công";
else
ViewData["thongbao"] = "Xóa không thành công";
return View();
}
}

StoreContext.cs

public class StoreContext


{
public string ConnectionString { get; set; }

public StoreContext(string connectionString)


{
ConnectionString = connectionString;
}
private MySqlConnection GetConnection()
{
return new MySqlConnection(ConnectionString);
}

public int InsertHanhKhach(HanhKhach hanhKhach)


{
using (MySqlConnection conn = GetConnection())
{
conn.Open();
var str = "insert into HANHKHACH values(@mahk, @hoten)";
MySqlCommand cmd = new MySqlCommand(str, conn);
cmd.Parameters.AddWithValue("mahk", hanhKhach.MaHK);
cmd.Parameters.AddWithValue("hoten", hanhKhach.HoTen);
return (cmd.ExecuteNonQuery());
}
}

public int ThemHnahKhach(CT_CB ct_cb)


{
using (MySqlConnection conn = GetConnection())
{
conn.Open();
var str = "insert into CT_CB values(@macb, @mahk)";
MySqlCommand cmd = new MySqlCommand(str, conn);
cmd.Parameters.AddWithValue("mahk", ct_cb.MaCB);
cmd.Parameters.AddWithValue("hoten", ct_cb.MaHK);
return (cmd.ExecuteNonQuery());
}
}

public ChuyenBay ThongTinChuyenBay(string maCB)


{
var result = new ChuyenBay();
using (MySqlConnection conn = GetConnection())
{
conn.Open();
var str = "select * from CHUYENBAY where MaCB=@macb";
MySqlCommand cmd = new MySqlCommand(str, conn);
cmd.Parameters.AddWithValue("macb", maCB);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{

result.MaCB = reader["MaCB"].ToString();
result.Chuyen = reader["Chuyen"].ToString();
result.Di = reader["Di"].ToString();
result.Den = reader["Den"].ToString();
result.GBay = reader["GBay"].ToString();
result.GDen = reader["GDen"].ToString();
result.Ngay = reader["Ngay"].ToString();
result.Thuong = reader["Thuong"].ToString();
result.Vip = reader["Vip"].ToString();
}

reader.Close();
}
}

return result;
}

public List<CT_CB> LietKeChuyenBay(string maCB)


{
var result = new List<CT_CB>();

using (MySqlConnection conn = GetConnection())


{
conn.Open();

var str =
"select HK.*, CT_CB.LoaiGhe, CT_CB.SoGHe from CT_CB, HANHKHACH
HK where MaCB=@macb and HK.MaHK = CT_CB.MaHK";
var cmd = new MySqlCommand(str, conn);
cmd.Parameters.AddWithValue("macb", maCB);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
result.Add(new CT_CB
{
LoaiGhe = bool.Parse(reader["LoaiGhe"].ToString()),
SoGhe = reader["SoGhe"].ToString(),
HanhKhach = new HanhKhach
{
MaHK = reader["MaHK"].ToString(),
HoTen = reader["Hoten"].ToString(),
DiaChi = reader["DiaChi"].ToString(),
DienThoai = reader["DienThoai"].ToString(),
}
});
}

reader.Close();
}

return (result);
}
}

public CT_CB ViewCT_CB(string makh, string macb)


{
var ct_cb = new CT_CB();
using (MySqlConnection conn = GetConnection())
{
conn.Open();
var str = "select CT_CB.*, HK.Hoten from CT_CB, HANHKHACH HK where
CT_CB.MaHk=@makh and MaCb = @macb and HK.Mahk = CT_CB.Mahk";
MySqlCommand cmd = new MySqlCommand(str, conn);
cmd.Parameters.AddWithValue("makh", makh);
cmd.Parameters.AddWithValue("macb", macb);
using (var reader = cmd.ExecuteReader())
{
reader.Read();
ct_cb.MaHK=reader["MaHK"].ToString();
ct_cb.MaCB = reader["MaCB"].ToString();
ct_cb.LoaiGhe = bool.Parse(reader["LoaiGhe"].ToString());
ct_cb.SoGhe = reader["SoGhe"].ToString();
ct_cb.HanhKhach = new HanhKhach
{
MaHK = reader["MaHK"].ToString(),
HoTen = reader["HoTen"].ToString()
};
}
}
return (ct_cb);

public int Update(CT_CB ct_cb)


{
using (MySqlConnection conn = GetConnection())
{
conn.Open();
var str = "update CT_CB set LoaiGhe = @loaighe, SoGhe = @soghe
where MaHK=@maHK and MaCB = @maCb";
MySqlCommand cmd = new MySqlCommand(str, conn);
cmd.Parameters.AddWithValue("loaighe", ct_cb.LoaiGhe);
cmd.Parameters.AddWithValue("soghe", ct_cb.SoGhe);
cmd.Parameters.AddWithValue("maHK", ct_cb.MaHK);
cmd.Parameters.AddWithValue("maCb", ct_cb.MaCB);
return (cmd.ExecuteNonQuery());
}
}

public int Delete(string makh, string macb)


{
using (MySqlConnection conn = GetConnection())
{
conn.Open();
var str = "delete from CT_CB where MaHK=@maHK and MaCB = @maCb";
MySqlCommand cmd = new MySqlCommand(str, conn);
return (cmd.ExecuteNonQuery());
}
}
}

View/HanhKhach/Insert.cshtml

@model HanhKhach
@{
ViewData["Title"] = "Thêm Hành Khách";
<form method="post" asp-controller="HanhKhach" asp-action="Enter">

<h1>Thêm hành khách</h1>


<label id="maHK">Mã hành khách</label><input type="text" asp-
for="MaHK" id="maHK"/><br/>
<label id="tenHK">Tên hành khách</label><input type="text" asp-
for="HoTen" id="tenHK"/><br/>
<label id="diachi">Địa Chỉ</label><input type="text" asp-for="DiaChi"
id="diachi"/><br/>
<label id="dienthoai">Điện thoại</label><input type="number" asp-
for="DienThoai" id="diathoai"/><br/>
<button type="submit" name="submit" value="Thêm">Thêm</button>
</form>
}

View/HanhKhach/Enter.cshtml

@model HanhKhach
@{
ViewData["Title"] = "Thông tin lấy được từ form";
}
@{

<p>Mã khoa:@Model.MaHK</p>
<p>Tên khoa:@Model.HoTen</p>

<p>@ViewData["ThongBao"]</p>
}

View/ChuyenBay/Index.cshtml

@model ChuyenBay

@{
ViewData["Title"] = "Xem thông tin máy bay";
<form method="get" asp-controller="ChuyenBay" asp-action="LietKe">
<h1>Xem thông tin chuyến bay</h1>
<label for="macb">Mã chuyến bay</label><input type="text" id="macb"
asp-for="MaCB" required/><br/>
<button type="submit" name="submit" value="Liệt kê">Liệt kê</button>
</form>
}

View/ChuyenBay/LietKe.cshtml

@model ChuyenBayViewModel
@{
ViewData["Title"] = "Liệt Kê Chuyến Bay";
<table>
<tr>
<td colspan="2">Thông tin chuyến bay</td>
</tr>
<tr>
<td>Chuyến <input type="text"
value="@Model.ChuyenBay.Chuyen" name="chuyen" disabled/></td>
<td>Ngày bay <input type="text"
value="@Model.ChuyenBay.Ngay" name="ngay" disabled/></td>
</tr>
<tr>
<td>Điểm Đi <input type="text"
value="@Model.ChuyenBay.Di" name="di" disabled/></td>
<td>Giờ Đi <input type="text"
value="@Model.ChuyenBay.GBay" name="giobay" disabled/></td>
</tr>
<tr>
<td>Điểm Đến <input type="text"
value="@Model.ChuyenBay.Den" name="den" disabled/></td>
<td>Giờ Đến <input type="text"
value="@Model.ChuyenBay.GDen" name="gioden" disabled/></td>
</tr>
<tr>
<td>Chỗ VIP <input type="text"
value="@Model.ChuyenBay.Vip" name="vip" disabled/></td>
<td>Chỗ Thường <input type="text"
value="@Model.ChuyenBay.Thuong" name="thuong" disabled/></td>
</tr>
<tr>
<td colspan="2">
<a href="/ChuyenBay/ThemHanhKhach" asp-
action="ThemHanhKhach" asp-route-id="@Model.ChuyenBay.MaCB">Thêm hành
khách</a>
</td>
</tr>

<tr>
<td colspan="2">
<table>
<thead>
<td>Họ tên hành khách</td>
<td>Điện thoại</td>
<td>Loại ghế</td>
<td>Số ghế</td>
<td>Chức năng</td>
</thead>

<tbody>
@foreach (var item in Model.ListCB)
{
<tr>
<td>@item.HanhKhach.HoTen</td>
<td>@item.HanhKhach.DienThoai</td>
<td>@item.LoaiGhe</td>
<td>@item.SoGhe</td>
<td>
<a href="Update?
makh=@item.MaHK&macb=@item.MaCB">Sửa</a>
<a href="Delete?
makh=@item.MaHK&macb=@item.MaCB">Xoá</a>
</td>
</tr>
}
</tbody>
</table>
</tr>
</table>

View/ChuyenBay/ThemHanhKhach.cshtml

@model ChuyenBay

@{
ViewData["Title"] = "Thêm hành khách";
<form method="get" asp-controller="ChuyenBay" asp-action="ThemHanhKhach">
<table>
<tr>
<td colspan="2">Thêm hành khách vào chuyến bay</td>
</tr>
<tr>
<td>Chuyến <input type="text" value="@Model.Chuyen"
name="chuyen" disabled/></td>
<td>Ngày bay <input type="text" value="@Model.Ngay"
name="ngay" disabled/></td>
</tr>
<tr>
<td>Điểm Đi <input type="text" value="@Model.Di" name="di"
disabled/></td>
<td>Giờ Đi <input type="text" value="@Model.GBay"
name="giobay" disabled/></td>
</tr>
<tr>
<td>Điểm Đến <input type="text" value="@Model.Den" name="den"
disabled/></td>
<td>Giờ Đến <input type="text" value="@Model.GDen"
name="gioden" disabled/></td>
</tr>
<tr>
<td>Chỗ VIP <input type="text" value="@Model.Vip" name="vip"
disabled/></td>
<td>Chỗ Thường <input type="text" value="@Model.Thuong"
name="thuong" disabled/></td>
</tr>
<tr>
<td>Mã hành khách <input type="text" name="mahk" /></td>
<td>Tên hành khách <input type="text" name="thuong" /></td>
</tr>
<tr>
<td>
Loại ghế
<select name="loaighe">
<option value="Thuong">Thường</option>
<option value="Vip">Vip</option>
</select>
</td>
<td>Số Ghế <input type="text" name="soghe" /></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" name="submit" value="Thêm">Thêm hành
khách</button>
</td>
</tr>
</table>
</form>
}

View/ChuyenBay/Update.cshtml
@model CT_CB

@{
ViewData["Title"] = "Update";
<form method="get" asp-controller="ChuyenBay" asp-action="Update">
<h1>Cập nhật thông tin hành khách</h1>
<label for="mahk">Mã Hành Khách</label><input type="text" id="mahk"
value="@Model.HanhKhach.MaHK"/><br/>
<label for="tenhk">Tên hành kháchh</label><input type="text"
id="tenhk" value="@Model.HanhKhach.HoTen"/><br/>
<label for="loaighe">Loại ghế</label>
<select id="loaighe" name="loaighe" asp-for="LoaiGhe">
<option value="Thuong">Thường</option>
<option value="Vip">Vip</option>
</select>
<label for="soghe">Số ghế</label><input type="text" id="soghe" asp-
for="SoGhe" value="@Model.SoGhe"/><br/>
<button type="submit" name="submit" value="Cập nhật">Cập nhật</button>
</form>
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy