//控制器
Service Service = new Service();
public IActionResult Index()
{
ViewData.Model = Service.GetMovieList();
return View();
}
public IActionResult Movie()
{
ViewData.Model = Service.GetMovieList();
return View();
}
public IActionResult ViewAutoLineRepairInfo(int page = 1, int pageSize = 4, string searchString = "")
{
List<View_AutoLineRepairInfo> allItems = Service.GetViewAutoLineRepairInfo(); // 获取所有数据
if (!String.IsNullOrEmpty(searchString))
{
allItems = allItems.Where(info => info.SerialNumber.Contains(searchString)).ToList();
}
int totalItems = allItems.Count();
int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
var items = allItems.Skip((page - 1) * pageSize).Take(pageSize).ToList();
ViewData["CurrentPage"] = page;
ViewData["TotalPages"] = totalPages;
ViewData["PageSize"] = pageSize; // 添加 PageSize 到 ViewData
ViewData["SearchString"] = searchString; // 将搜索关键字存入 ViewData
ViewData.Model = items;
return View();
}
@{
ViewData["Title"] = "首页";
}
<h1>index</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<td>报表名称</td>
</tr>
</thead>
<tbody id="content">
<tr>
<td><a href="/Home/Movie">Movie</a></td>
</tr>
<tr>
<td><a href="/Home/ViewAutoLineRepairInfo">View_AutoLineRepairInfo</a></td>
</tr>
</tbody>
</table>
@{
ViewData["Title"] = "Movie";
}
<h1>Movie</h1>
<br />
<table class="table table-bordered table-striped">
<thead>
<tr>
<td>Id</td>
<td>Title</td>
<td>ReleaseDate</td>
<td>Genre</td>
<td>Price</td>
</tr>
</thead>
<tbody id="content">
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.Title</td>
<td>@item.ReleaseDate</td>
<td>@item.Genre</td>
<td>@item.Price</td>
</tr>
}
</tbody>
</table>
@{
ViewData["Title"] = "ViewAutoLineRepairInfo";
}
@using (Html.BeginForm("ViewAutoLineRepairInfo", "Home", FormMethod.Get))
{
<p>
SerialNumber: <input type="text" name="searchString">
<input type="submit" value="搜索" />
</p>
}
<h1>ViewAutoLineRepairInfo</h1>
<div style="overflow-x: auto;">
<table class="table table-bordered table-striped">
<!-- 表头 -->
<thead>
<tr>
<td> ID</td>
<td> SerialNumber</td>
<td> LineCode</td>
<td> OffLineGroupCode</td>
<td> OffLineUser</td>
<td> OffLineReason</td>
<td> OffLineTime</td>
<td> PrintCount</td>
<td> Identifier</td>
<td> VerifyReason</td>
<td> ProType</td>
<td> BadCode</td>
<td> VerifyTime</td>
<td> RouteName</td>
<td> EntryGroupCode</td>
<td> OnLineGroupCode</td>
<td> OnLineConfirmInfo</td>
<td> OnLineUser</td>
<td> OnLineTime</td>
<td> State</td>
</tr>
</thead>
<tbody id="content">
<!-- 数据行 -->
@foreach (var item in Model)
{
<tr>
<td>@item.ID</td>
<td>@item.SerialNumber</td>
<td>@item.LineCode</td>
<td>@item.OffLineGroupCode</td>
<td>@item.OffLineUser</td>
<td>@item.OffLineReason</td>
<td>@item.OffLineTime</td>
<td>@item.PrintCount</td>
<td>@item.Identifier</td>
<td>@item.VerifyReason</td>
<td>@item.ProType</td>
<td>@item.BadCode</td>
<td>@item.VerifyTime</td>
<td>@item.RouteName</td>
<td>@item.EntryGroupCode</td>
<td>@item.OnLineGroupCode</td>
<td>@item.OnLineConfirmInfo</td>
<td>@item.OnLineUser</td>
<td>@item.OnLineTime</td>
<td>@item.State</td>
</tr>
}
</tbody>
</table>
</div>
<!-- 分页导航 -->
<nav aria-label="Page navigation">
<ul class="pagination">
@if ((int)ViewData["TotalPages"] > 1)
{
@if ((int)ViewData["CurrentPage"] > 1)
{
<li class="page-item">
<a class="page-link" href="@Url.Action("ViewAutoLineRepairInfo", new { page = 1, pageSize = (int)ViewData["PageSize"] })">最前</a>
</li>
}
@for (int i = Math.Max(1, (int)ViewData["CurrentPage"] - 2); i <= Math.Min((int)ViewData["TotalPages"], (int)ViewData["CurrentPage"] + 2); i++)
{
<li class="page-item @(i == (int)ViewData["CurrentPage"] ? "active" : "")">
<a class="page-link" href="@Url.Action("ViewAutoLineRepairInfo", new { page = i, pageSize = (int)ViewData["PageSize"] })">@i</a>
</li>
}
@if ((int)ViewData["CurrentPage"] < (int)ViewData["TotalPages"])
{
<li class="page-item">
<a class="page-link" href="@Url.Action("ViewAutoLineRepairInfo", new { page = (int)ViewData["TotalPages"], pageSize = (int)ViewData["PageSize"] })">最后</a>
</li>
}
}
</ul>
</nav>
类
public class Service:SqlsugarBase
{
public List<Movie> GetMovieList()
{
List<Movie> list = new List<Movie>();
list = db.Queryable<Movie>().ToList();
return list;
}
public List<View_AutoLineRepairInfo> GetViewAutoLineRepairInfo()
{
List<View_AutoLineRepairInfo> list = new List<View_AutoLineRepairInfo>();
list = Idb.Queryable<View_AutoLineRepairInfo>("View_AutoLineRepairInfo").ToList();
return list;
}
}
using SqlSugar;
namespace WebApp.Models
{
public class SqlsugarBase
{
/// <summary>
/// 读取json配置文件
/// </summary>
private static IConfigurationRoot Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
/// <summary>
/// 读取配置文件下的连接字符串
/// </summary>
string connectionString = Configuration.GetSection("ConnectionStrings").GetSection("BaseDbSqlServer").Value;
string IconnectionString= Configuration.GetConnectionString("IBaseDbSqlServer");
public SqlSugarClient db => GetInstance();
public SqlSugarClient Idb => IGetInstance();
SqlSugarClient GetInstance()
{
var db = new SqlSugarClient(
new ConnectionConfig
{
ConnectionString = connectionString,
DbType = DbType.SqlServer,//数据库类型
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
}
);
return db;
}
SqlSugarClient IGetInstance()
{
var db = new SqlSugarClient(
new ConnectionConfig
{
ConnectionString = IconnectionString,
DbType = DbType.SqlServer,//数据库类型
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
}
);
return db;
}
}
}
配置
css
/* site.css */
.btn-submit {
background-color: #4CAF50;
color: white;
padding: 4px 20px;
border: none;
border-radius: 5px;
}
/* 使表格单元格内容自动换行 */
.table {
table-layout: auto;
word-wrap: break-word;
white-space: nowrap;
}