正在 ASP.NET Core Razor 页面中获取 SQL 服务器 table 数据

Fetching SQL Server table data in ASP.NET Core Razor pages

我试图在用户访问我的 ASP.NET 核心应用程序(使用 Razor 页面)中的 Index 页面时获取一些 SQL 数据。在我的 Index.cshtml.cs 中,我试图建立与 SQL 服务器的连接并将行存储在列表中。现在我想在 Index.cshtml 中获取此列表。我不确定这种方法,但这是我尝试过的方法..

这是我的Index.cshtml.cs

namespace SQLICMAlertWebApp.Pages
{
    [Authorize]
    public class IndexModel : PageModel
    {
        public IConfiguration Configuration { get; }

        public IndexModel(IConfiguration configuration)
        {
            Configuration = configuration;
            con.ConnectionString = Configuration.GetConnectionString("OEM_US_SQL_CORL");
        }

        SqlCommand com = new SqlCommand();
        SqlDataReader dr;
        SqlConnection con = new SqlConnection();
        List<ProActive_Monitoring_Missing_PVR_Orders> table_errors = new List<ProActive_Monitoring_Missing_PVR_Orders>();

        public void OnGet()
        {
            if (table_errors.Count > 0)
            {               
                table_errors.Clear();
            }

            try
            {
                con.Open();
                com.Connection = con;
                com.CommandText = "SELECT * FROM ProActiveMonitoring_Missing_PVR_Orders WHERE isICMSent = 'No'";

                dr = com.ExecuteReader();

                while (dr.Read())
                {
                    table_errors.Add(new ProActive_Monitoring_Missing_PVR_Orders()
                    {
                        CustomerNbr = dr["CustomerNbr"].ToString(),
                        CustomerName = dr["CustomerName"].ToString(),
                        MSOrderNumber = dr["MSOrderNumber"].ToString(),
                        Quantity = dr["Quantity"].ToString(),
                        PromoQuantity = dr["PromoQuantity"].ToString(),
                        QtyDiff = dr["QtyDiff"].ToString(),
                        NetAmount = dr["NetAmount"].ToString(),
                        PromoNetAmount = dr["PromoNetAmount"].ToString(),
                        AmtDiff = dr["AmtDiff"].ToString(),
                        ExtendedAmount = dr["ExtendedAmount"].ToString(),
                        PromoExtendedAmount = dr["PromoExtendedAmount"].ToString(),
                        IsICMSent = dr["IsICMSent"].ToString()
                    });
                }

                con.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex);
            }
        }
    }
}

这里是 Index.cshtml 从上面定义的 table_errors 变量中检索数据。

@page
@model SQLICMAlertWebApp.Pages.IndexModel
@{
    ViewData["Title"] = "Home";
    string[] TableHeaders = new string[] {"CustomerNbr"
                              ,"CustomerName"
                              ,"MSOrderNumber"
                              ,"Quantity"
                              ,"PromoQuantity"
                              ,"QtyDiff"
                              ,"NetAmount"
                              ,"PromoNetAmount"
                              ,"AmtDiff"
                              ,"ExtendedAmount"
                              ,"PromoExtendedAmount"};
    bool checkEmty = true;
}

<div class="HomePage">
    <div class="myNavbar">
        <div class="header-title">SQL Data Mismatch Tables</div>
        <button class="LogoutButton" onclick={location.replace('/home/logout')}>Logout</button>
    </div>
    <div class="HomePageBody">
    <div class="table">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th class="th_table_name" colspan="11">
                        ProActive_Monitoring_Missing_PVR_Orders_Errors
                    </th>
                </tr>
                <tr>
                    @{
                        foreach (var head in TableHeaders)
                        {
                            <th>
                                @head
                            </th>
                        }
                    }
                </tr>
            </thead>

            <tbody>
                @{
                    if (Model != null)
                    {
                        foreach (var Data in table_errors)
                        {
                            checkEmty = false;
                            <tr>
                                <td>@Data.CustomerNbr</td>
                                <td>@Data.CustomerName</td>
                                <td>@Data.MSOrderNumber</td>
                                <td>@Data.Quantity</td>
                                <td>@Data.PromoQuantity</td>
                                <td>@Data.QtyDiff</td>
                                <td>@Data.NetAmount</td>
                                <td>@Data.PromoNetAmount</td>
                                <td>@Data.AmtDiff</td>
                                <td>@Data.ExtendedAmount</td>
                                <td>@Data.PromoExtendedAmount</td>
                            </tr>
                        }
                        if (checkEmty)
                        {
                             <tr>
                                <td colspan="11">No Errors</td>
                            </tr>
                        }
                    }
                }
            </tbody>
        </table>
        </div>
    </div>
</div>

我不确定它是否应该以这种方式工作,但 Index.cshtml 无法识别 table_errors 变量。如果这不正确,我应该改变我的方法!!

编辑 #2

table_errors 更改为 Model.table_errors 后效果很好!!但是我在代码中又遇到了一个错误,你知道吗?

Razor Pages 使用 MVVM 模式。所以如果你想访问视图中的对象(在本例中:Index.cshtml),你应该通过模型访问对象。 例如,要在视图中使用 table_errors,请按以下方式执行:@Model.table_errors

此代码有效:

<tbody>
            @{
                if (Model.table_errors != null)
                {
                    foreach (var Data in Model.table_errors)
                    {
                        checkEmty = false;
                        <tr>
                            <td>@Data.CustomerNbr</td>
                            <td>@Data.CustomerName</td>
                            <td>@Data.MSOrderNumber</td>
                            <td>@Data.Quantity</td>
                            <td>@Data.PromoQuantity</td>
                            <td>@Data.QtyDiff</td>
                            <td>@Data.NetAmount</td>
                            <td>@Data.PromoNetAmount</td>
                            <td>@Data.AmtDiff</td>
                            <td>@Data.ExtendedAmount</td>
                            <td>@Data.PromoExtendedAmount</td>
                        </tr>
                    }
                    if (checkEmty)
                    {
                         <tr>
                            <td colspan="11">No Errors</td>
                        </tr>
                    }
                }
            }
        </tbody>