一、控制器对应的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using HotelLiveProject.Models;
using Microsoft.EntityFrameworkCore.Query;
namespace HotelLiveProject.Controllers
{
public class BookingRoomsController : Controller
{
private readonly ILogger<BookingRoomsController> _logger;
private readonly HotelContext _context;
private readonly IHttpContextAccessor _httpContextAccessor; //请求对象
public BookingRoomsController(ILogger<BookingRoomsController> logger, HotelContext context, IHttpContextAccessor httpContextAccessor)
{
_logger = logger;
_context = context;
_httpContextAccessor = httpContextAccessor; //使用session
}
//我的房间预定
[TypeFilter(typeof(CustomActionFilterAttribute))]
public async Task<IActionResult> MyIndex(String keyword = "", String sort = "desc", int page = 1)
{
int uid = (int)_httpContextAccessor.HttpContext.Session.GetInt32("id");
IEnumerable <BookingRoom> list = _context.BookingRooms.Include(b => b.HidNavigation).Include(b => b.UidNavigation);
//实现模糊搜索
if (!String.IsNullOrEmpty(keyword))
{
list = list.Where(p => p.HidNavigation.Htitle.Contains(keyword) && p.Uid == uid);
}
else
{
list = list.Where(p => p.Uid == uid);
}
//实现排序
if (sort == "desc")
{
list = list.OrderByDescending(p => p.IsPay).OrderByDescending(p => p.Id);
}
else
{
list = list.OrderByDescending(p => p.IsPay).OrderBy(p => p.Id);
}
int pageSize = 10; //10个数据一页
//实现分页
int recordCount = list.Count();
list = list.Skip((page - 1) * pageSize).Take(pageSize);
//(3)获取当前可以分多少页的页码数回去
ViewBag.pageNum = Math.Ceiling((Convert.ToDecimal(recordCount)) / (Convert.ToDecimal(pageSize)));
return _context.Homestays != null ?
View(list.ToList()) :
Problem("Entity set 'HotelContext.Homestays' is null.");
}
// 预定列表
[TypeFilter(typeof(CustomActionFilterAttribute))]
public async Task<IActionResult> Index(String keyword = "", String sort = "desc", int page = 1)
{
IEnumerable<BookingRoom> list = _context.BookingRooms.Include(b => b.HidNavigation).Include(b => b.UidNavigation);
//实现模糊搜索
if (!String.IsNullOrEmpty(keyword))
{
list = list.Where(p => p.UidNavigation.Nickname.Contains(keyword));
}
//实现排序
if (sort == "desc")
{
list = list.OrderByDescending(p => p.IsPay).OrderByDescending(p => p.Id);
}
else
{
list = list.OrderByDescending(p => p.IsPay).OrderBy(p => p.Id);
}
int pageSize = 10; //10个数据一页
//实现分页
int recordCount = list.Count();
list = list.Skip((page - 1) * pageSize).Take(pageSize);
//(3)获取当前可以分多少页的页码数回去
ViewBag.pageNum = Math.Ceiling((Convert.ToDecimal(recordCount)) / (Convert.ToDecimal(pageSize)));
return _context.Homestays != null ?
View(list.ToList()) :
Problem("Entity set 'HotelContext.Homestays' is null.");
}
// 创建预定
//public IActionResult Create()
//{
// ViewData["Hid"] = new SelectList(_context.Homestays, "Hid", "Hid");
// ViewData["Uid"] = new SelectList(_context.Users, "Id", "Id");
// return View();
//}
// 保存预定
[HttpPost]
//public async Task<IActionResult> Create([FromBody] int Uid,int Hid,DateTime StartTime, DateTime EndTime, String Mark,short IsPay,String Comment,decimal Hprice)
[TypeFilter(typeof(CustomActionFilterAttribute))]
public async Task<IActionResult> Create([FromBody] BookingRoom bookingRoom)
{
_context.Add(bookingRoom);
var res = await _context.SaveChangesAsync();
if (res > 0) {
return Content("200");
}
else
{
return Content("201");
}
}
// 支付客房
[TypeFilter(typeof(CustomActionFilterAttribute))]
public async Task<IActionResult> Edit(int? id)
{
try
{
BookingRoom bookingRoom = _context.BookingRooms.Where(p => p.Id == id).FirstOrDefault();
bookingRoom.IsPay = 1; //改为已支付
_context.Update(bookingRoom); //执行更改支付状态
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
}
return RedirectToAction(nameof(MyIndex));
}
// 删除预定
[TypeFilter(typeof(CustomActionFilterAttribute))]
public async Task<IActionResult> Delete(int id)
{
if (_context.BookingRooms == null)
{
return Problem("Entity set 'HotelContext.BookingRooms' is null.");
}
var bookingRoom = await _context.BookingRooms.FindAsync(id);
if (bookingRoom != null)
{
_context.BookingRooms.Remove(bookingRoom);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
// 删除我的预定
[TypeFilter(typeof(CustomActionFilterAttribute))]
public async Task<IActionResult> Delete1(int id)
{
if (_context.BookingRooms == null)
{
return Problem("Entity set 'HotelContext.BookingRooms' is null.");
}
var bookingRoom = await _context.BookingRooms.FindAsync(id);
if (bookingRoom != null)
{
_context.BookingRooms.Remove(bookingRoom);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(MyIndex));
}
private bool BookingRoomExists(int id)
{
return (_context.BookingRooms?.Any(e => e.Id == id)).GetValueOrDefault();
}
}
}
对应的前端代码参考:
@model IEnumerable<HotelLiveProject.Models.BookingRoom>
@{
ViewData["Title"] = "客房预定";
Layout = "~/Views/Shared/_Top.cshtml";
}
<style>
a {
text-decoration: none;
}
.sear {
}
.kehu {
text-align: center;
}
.content1 {
margin-top: 10px;
}
#sub{
margin-top:20px;
margin-left:20px;
}
.table1{
margin-left:50px;
}
.previous{
text-decoration: none;
display: inline-block;
padding: 8px 16px;
}
.btn{
display:inline-block;
padding:10px;
border: 1px black solid;
background: black;
color: white;
}
</style>
<body>
<div class="content1">
<div class="sear">
<div class="kehu">
<h1>预定管理</h1>
</div>
<hr />
<div id="sub">
<form method="get">
客户姓名:<input type="text" name="keyword" value="@(!String.IsNullOrEmpty(Context.Request.Query["keyword"]) ? Context.Request.Query["keyword"] : "")" style="width: 200px; height: 25px;" />
根据id排序:
<select name="sort">
<option value="desc">倒序</option>
<option value="asc">升序</option>
</select>
<input type="submit" name="btn" value="查询" style="background-color: black; color: white; width: 60px; height: 28px; margin-left: 10px " />
</form>
</div>
<div class="table1">
<table class="tables" border="1" cellspacing="0" Padding=0 style="width:1150px;height:100px;margin-top:20px;text-align:center;">
<thead>
<tr>
<th>
Id
</th>
<th>
入住客房名
</th>
<th>
入住时间
</th>
<th>
退房时间
</th>
<th>
备注
</th>
<th>
是否支付
</th>
<th>
客房金额
</th>
<th>
入住人名字
</th>
<th>
入住人身份证
</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@item.Id
</td>
<td>
@item.HidNavigation.Htitle
</td>
<td>
@Html.DisplayFor(modelItem => item.StartTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mark)
</td>
<td>
@if(item.IsPay == 1)
{
<b style="color:red;">已支付</b>
}
else
{
<b>未支付</b>
}
</td>
<td>
¥@Html.DisplayFor(modelItem => item.Hprice)元
</td>
<td>
@item.UidNavigation.Nickname
</td>
<td>
@item.UidNavigation.Idnumber
</td>
<td>
<a asp-action="Delete" class="btn" onclick="return confirm('是否取消该预约?')" asp-route-id="@item.Id">取消预约</a>
</td>
</tr>
}
</tbody>
</table>
<div class="previous" style="margin-left:500px;margin-top:20px;padding-bottom:50px;"
<center>
<style>
.tpl-pagination {
display: flex;
justify-content: center;
margin: 20px;
}
.tpl-pagination li {
border: 1px solid #ccc;
border-radius: 10px;
height: 40px;
line-height: 40px;
padding: 0 10px;
text-align: center;
margin: 5px;
list-style:none;
}
</style>
<ul class="tpl-pagination">
@if (ViewBag.pageNum == null | ViewBag.pageNum == 0)
{
<li class="am-active">
<a href="?page=1&sort=@(Context.Request.Query["sort"])&keyword=@(!String.IsNullOrEmpty(Context.Request.Query["keyword"]) ? Context.Request.Query["keyword"] : "")">1</a>
</li>
}
@for (var i = 1; i <= ViewBag.pageNum; i++)
{
<li>
<a href="?page=@i&sort=@(Context.Request.Query["sort"])&keyword=@(!String.IsNullOrEmpty(Context.Request.Query["keyword"]) ? Context.Request.Query["keyword"] : "")">@i</a>
</li>
}
</ul>
</center>
</div>
</div>
</div>
</div>
</body>
结果展示: