簡體   English   中英

如何創建 model 並基於返回 pivot 表的 SQL 服務器存儲過程查看?

[英]How do I create a model and view based on a SQL Server stored procedure that returns a pivot table?

我有一個非常復雜的存儲過程,它返回 pivot 數據表。 該數據是動態的。 列名會改變,列數也會改變。 基於為特定醫師審核的圖表審核數量。 這就是 SQL 服務器從存儲過程中為特定醫師返回數據的方式。

Question                A    B      C    D     Average
--------------------------------------------------------
11_ChartWellOrg         5.0  6.0    6.0  6.0    5.8
12_HistInfoAdequate     5.0  6.0    5.0  5.0    5.3
14_PhysExamAdequate     6.0  6.0    6.0  6.0    6.0
16_AssessFormulation    6.0  6.0    5.0  6.0    5.8
18_PlanTreatAdequate    6.0  6.0    6.0  6.0    6.0
20_GlobalOverallAssess  6.0  6.0    6.0  6.0    6.0

過去,我只是把它放在一個 DataGrid 中,我可以用頁面后面的代碼來處理動態部分。

Public Sub BindGridView()
    Dim tc As New TestCode
    Dim dt As New DataTable

    If intType <> 3 Then
        dt = tc.GetDT(intParticipant, intActivity, intAuditor, dBatch)
    Else
        dt = tc.GetDT(intParticipant, intActivity, intAuditor, intMonth, intYrChartEntry, intDept)
    End If
    intRows = dt.Rows.Count
    intColumns = dt.Columns.Count

    gvPivot.DataSource = dt
    gvPivot.DataBind()
    gvPivot.HeaderRow.Visible = True
    gvPivot.HeaderRow.BackColor = Drawing.Color.FromArgb(79, 113, 185)
    GridFormat()

End Sub

現在的要求是我將其移至我們使用 ASP.NET Core 3.1 MVC 構建的 web 門戶。 我想我可以將其加載到 DataTable 中並將其發送到此視圖:

@using System.Data
@model DataTable

@{
}
<div>
    <div>
        <h4>Matrix</h4>
        <table id="tblData" class="table">
            <thead>
                <tr>
                    @foreach(DataColumn col in Model.Columns)
                    {
                        <th>@col.ColumnName</th>
                    }
                </tr>
            </thead>
            <tbody>
                @foreach (DataRow row in Model.Rows)
                {
                    <tr>
                        @foreach (DataColumn col in Model.Columns)
                        {
                            <td>@row[col.ColumnName]</td>
                        }
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

我認為這將處理提供的 DataTable 的動態部分。

我想不通的是如何使用我的 DBContext 獲取數據。 我不認為這可以處理動態表或模型。 我可以在 DBContext 周圍 go 並將這些數據拉到 DBContext 之外嗎? 這是不好的形式嗎? 我是 MVC 和實體框架的新手。 到目前為止,我在門戶網站上做得很好,這個只是在頭和脖子上打敗了我。 建議? 幫助? 毒品?

如果我需要提供更多信息,請告訴我。 我通常不在論壇上發帖,因為答案似乎已經存在,我不必問這個問題。

所以答案很簡單,我想多了。 將數據放入 DataTable,然后讓 View 處理動態部分就足夠了。 這就是我獲得動態存儲過程數據的方式:

var dt = new DataTable();

using (var command = context.Database.GetDbConnection().CreateCommand())
{
    command.CommandText = "YourStoredProcedureName";
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue(...);
    
if (command.Connection.State != ConnectionState.Open)
{
    command.Connection.Open();
}

using (var reader = command.ExecuteReader())
{
    dt.Load(reader);
}
}

我使用 Controller 將其放入正確的 model 中:

[Route("[Controller]/[Action]/{IDParticipant_2}/{IDActivity}/{IDAuditor_5}/{BatchDate}")]
public async Task<ActionResult> ChartAuditMatrix(int IDParticipant_2, int IDActivity, int IDAuditor_5, DateTime BatchDate)
{
    DataTable table = await _chartAuditRepo.GetMatrix(IDParticipant_2, IDActivity, IDAuditor_5, BatchDate);
    var overallAssess = await _chartAuditRepo.GetOverallAssessmentNote(IDParticipant_2, IDActivity, IDAuditor_5, BatchDate);
    ViewBag.BatchDate = BatchDate;

    var model = new MatrixVM
    {
        Matrix = table,
        AllAuditAssessment = overallAssess
    };

    return View(model);
}

這是 model ,它有兩個屬性,一個是包含上面 pivot 表的 DataTable:

public class MatrixVM
{
    public DataTable Matrix { get; set; }
    public AllAuditAssessment AllAuditAssessment { get; set; }
}

然后我使用 Model 作為處理 DataTable 結果的動態部分的視圖的基礎:

@using System.Data
@model MatrixVM

<div class="row">
    <div class="col-md-3 offset-1">
        <table id="tblMatrix" class="table" style="border: solid; background-color: white;">
            <thead style="background-color: darkgray;">
                <tr>
                    @foreach (DataColumn col in Model.Matrix.Columns)
                    {
                        @if (col.ColumnName == "ZZ_Average")
                        {

                            <th>Average</th>
                        }
                        else
                        {
                            <th>@col.ColumnName</th>
                        }

                    }
                </tr>
            </thead>
            <tbody>
                @foreach (DataRow row in Model.Matrix.Rows)
                {
                    <tr>
                        @foreach (DataColumn col in Model.Matrix.Columns)
                        {
                            <td>
                                @row[col.ColumnName]
                            </td>
                        }
                    </tr>
                }
            </tbody>
        </table>
    </div>

把這些東西想出來,不讓應該如何做的想法妨礙我,有時可能是我的失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM