asp mvc .net6 报表分页 查询

文章描述了一个使用ASP.NETMVC开发的Web应用中,控制器中的Service类如何通过SqlSugarORM进行数据库操作,包括获取电影列表和View_AutoLineRepairInfo数据,并实现分页功能。
摘要由CSDN通过智能技术生成

 

//控制器
 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值