网页上的ASP.net核心表

问题描述 投票:0回答:1

亲爱的,
我正在使用 C# 使用 .NET6.0 创建一个 ASP.NET Core Web 应用程序。核。网页返回结果有问题,我的sql没问题。我才刚刚开始,希望完成与现有 SQL 数据库的连接。在调试中,我看到我的表已加载 (topscoorder.cshtml.cs),但它没有显示在我的网页 (topscoorder.cshtml) 上。我必须做什么才能显示结果?

Topscoorder.chtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Http;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System;
using System.Reflection.PortableExecutable;
using System.Data;

namespace Tournament_organizer.Pages
{
public class TopscoorderModel : PageModel`
{
public string? sessionvariable;
public Guid variabelIDdivisie;
public string? variabelToer;
public string? variabelTopScoorder;
public Int32 variabelAantal;
public Int32 variabelAantalSpeler;
public Int32 variabelAantalDoelpunten;
public Int32 variabelAantalKaarten;
public Int32 variabelDoelpunten;
public String variabelVlag;
public String variabelTeam;
public List <DivisieTotaal> DivisiesTjes = new();
public List <TopscoorderTotaal> variabelTopscoorders = new();

public void OnGet()
{
List\<DivisieTotaal\> DivisiesTjes = new List\<DivisieTotaal\>();
sessionvariable = "'25ed9970-ff7a-4750-a35f-1f9f5ec4b286'";

//Set value in Session object.
if (HttpContext.Session.GetString("SessionVar") != null)
sessionvariable = HttpContext.Session.GetString("SessionVar");

try
{
String connectionString = "*************";using SqlConnection connection = new(connectionString);
connection.Open();

//inladen van de divisies met de topscoorders
String sql = "SELECT \* FROM dbo.division WHERE id_toernooi='25ed9970-ff7a-4750-a35f-1f9f5ec4b286'and aktief='J' ORDER BY Naam";

SqlDataAdapter DivAdapter = new SqlDataAdapter(sql, connection);
DataSet dsDivisie = new DataSet();
DivAdapter.Fill(dsDivisie, "Divisie");

foreach (DataTable ODTdiv in dsDivisie.Tables)
{
foreach (DataRow ODRdiv in ODTdiv.Rows)
{
variabelIDdivisie = (Guid)ODRdiv\["id"\];
variabelTopScoorder = "To be confirmed";
List\<TopscoorderTotaal\> variabelTopscoorders = new List\<TopscoorderTotaal\>();

//inladen van de topscoorders                       
sqlT = "select  vlag, speler.naam, team.naam as team, sum(doelpunten) as doelpunten, sum(rodekaarten) as rode_kaarten, sum(gelekaarten) as gele_kaarten, sum(groenekaarten) as groene_kaarten  from speler, speler_game, game, division, land, league, team where speler.id = speler_game.id_speler and speler_game.id_game = game.id and game.id_division = division.id    and division.aktief = 'J' and game.aktief = 'J' and speler.aktief = 'J'    and land.id = league.id_land   and league.id = speler.id_league   and speler.id_team = team.id   and division.id =   '" + variabelIDdivisie.ToString() + "'   group by  land.vlag, speler.naam, team.naam order by sum(doelpunten) desc, sum(rodekaarten) asc, sum(gelekaarten) asc, sum(groenekaarten) asc";

SqlDataAdapter adapter = new SqlDataAdapter(sqlT, connection);
DataSet dsTopscoorder = new DataSet();
adapter.Fill(dsTopscoorder, "Topscoorder");

foreach (DataTable ODT in dsTopscoorder.Tables)
{
foreach (DataRow ODR in ODT.Rows)
{
variabelTopScoorder = (string)ODR\["naam"\];
variabelDoelpunten = (Int32)ODR\["doelpunten"\];
variabelVlag = (String)ODR\["vlag"\];
variabelTeam = (String)ODR\["team"\];

TopscoorderTotaal topscoorderTotaal = new()
{
Doelpunten = variabelDoelpunten,
Vlag = variabelVlag,
Team = variabelTeam,
Topscoorder = variabelTopScoorder
};
variabelTopscoorders.Add(topscoorderTotaal);
};
DivisieTotaal divisieTotaal = new()
{
id_division = variabelIDdivisie,
Naam = (String)ODRdiv\["naam"\],
Shotklok = (String)ODRdiv\["shotklok"\],
Topscoorders = variabelTopscoorders
};
DivisiesTjes.Add(divisieTotaal);    
}
}
}
}

catch (Exception ex)
{
Console.WriteLine("NOT Connected");  // something like this
Console.WriteLine("Exception: connectie NIET ok " + ex.Message);
}
}
}
public class DivisieTotaal
{
public String? Naam;
public String? Shotklok;
public Guid id_division;
public List \<TopscoorderTotaal\>? Topscoorders;
}

public class TopscoorderTotaal
{
public String? Topscoorder;
public Int32 Doelpunten;
public String? Vlag;
public String? Team;
}
}

webpagina.cshtml

@page
@model Tournament_organizer.Pages.TopscoorderModel
@using Microsoft.AspNetCore.Http
@addTagHelper\*, Microsoft.AspNetCore.Mvc.TagHelpers
@using Microsoft.Extensions.Configuration;

@{
Layout = "\_Layout_Overzicht";
}
<row class="eigentabel">
<p>@DateTime.Now</p>
<row>
<div class="container">
<div class="row">
@foreach (var item in Model.DivisiesTjes)
{
//hier zetten we de titel op de pagina
<h2>@item.Naam </h2>

<table id="" class="display">
<thead>
<tr>
<th></th>
<th>Player name</th>
<th>Country</th>
<th></th>
</tr>
</thead>
<tbody>


</tbody>
</table>
}
</div>
</div>
</row>
</row>

数据sql数据库:

c# asp.net-core datatable dataset strongly-typed-dataset
1个回答
0
投票

在调试中,我看到我的表已加载 (topscoorder.cshtml.cs) 但它没有出现在我的网页 (topscoorder.cshtml) 上。我该怎么办 必须做什么才能显示结果?

实际上,基于您共享的代码片段,这真的很难阅读并且不符合惯例或良好实践。

显然,我看到了一些可能导致您的视图页面上没有投影的错误。此外,尝试在小卡盘中拆分较大的方法以增加可读性。

让我们来看看一些主要问题。

首先,我们必须定义 property 以将值绑定到它,以便我们可以访问方法之外的那些。不幸的是,您只定义了这些 fields,它们无法从视图中访问。

因此,您应该在类中定义您的属性。您可以执行以下操作:

型号:

public class TopscoorderTotal
    {
        public string? Topscoorder { get; set; }
        public int Doelpunten { get; set; }
        public string? Vlag { get; set; }
        public string? Team { get; set; }
    }

cshtml.cs:

public class ReadFromDataTableModel : PageModel
    {
        
        public List<TopscoorderTotal>? TopscoorderTotals { get; set; }
        public void OnGet()
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                var query = "SELECT Topscoorder ,Doelpunten ,Vlag ,Team FROM TopscoorderTotaal";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.CommandType = CommandType.Text;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                con.Close();
                string serializeObject = JsonConvert.SerializeObject(dt);
                var dataTableObjectInPOCO = JsonConvert.DeserializeObject<List<TopscoorderTotal>>(serializeObject);
                TopscoorderTotals = dataTableObjectInPOCO;
            }
        }
    }

注意: 如您所见,我们已经初始化了一个列表 TopscoorderTotals,它将在视图中使用以循环遍历集合。此外,在获取数据表值后,我们将其序列化为 poco 类。除此之外,我只为 TopscoorderTotal 类准备了上述演示,您现在也可以结合 DivisieTotal。不如建一个view model然后绑定在一起。这样会容易得多。

cshtml:

@page
@model ReadFromDataTableModel
<table class="table">
    <thead>
        <tr>
            <th>Topscoorder
            <th>Doelpunten
            <th>Vlag
            <th>Team
           
        </tr>
    </thead>
    <tbody>
        @foreach (var items in @Model.TopscoorderTotals!)
        {
            <tr>
                <td>@items.Topscoorder</td>
                <td>@items.Doelpunten</td>
                <td>@items.Vlag</td>
                <td>@items.Team</td>
            </tr>
        }
    </tbody>
</table>

输出:

注意:我建议你学习一下entity framework,因为它可以让你更方便地做上面的事情。

© www.soinside.com 2019 - 2024. All rights reserved.