在具有多个数组列表<>的 ASP.Net Core 应用程序上显示 SQL 数据 [重复]
Posted
技术标签:
【中文标题】在具有多个数组列表<>的 ASP.Net Core 应用程序上显示 SQL 数据 [重复]【英文标题】:Displaying SQL Data on a ASP.Net Core Application with Multiple Array List<> [duplicate] 【发布时间】:2021-09-06 06:38:51 【问题描述】:我找到了一篇关于如何通过“public IActionResult Index()”传递一个包含 sql 数据的数组列表的教程,但我需要多个列表,其中包含通过我的 index.cshtml 页面传递的多个 sql 查询语句的数据。我正在使用 ASP.Net Core MVC。我有多个要展示的模型和 HomeController。我也可以显示cshtml。我试图通过一个 List 传递所有查询语句,但它会打印出多个第一个查询语句为空。
CampaignCreat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CampaignReportDemo.Models
public class CampaignCreat
public string CampaignCreative get; set;
WebBrowser.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CampaignReportDemo.Models
public class WebBrowser
public string SoftwareName get; set;
public string number1 get; set;
DeviceClick.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CampaignReportDemo.Models
public class DeviceClick
public string HardwareType get; set;
public string number get; set;
CampaignSummary.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CampaignReportDemo
public class CampaignSummary
public string FriendlyFrom get; set;
public string BroadcastDate get; set;
public string EmailsOrdered get; set;
public string Opens get; set;
public string Clicks get; set;
HomeController.cs
using CampaignReportDemo.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace CampaignReportDemo.Controllers
public class HomeController : Controller
SqlCommand com = new SqlCommand();
SqlDataReader dr;
SqlConnection con = new SqlConnection();
List<CampaignSummary> CampaignSummaries = new List<CampaignSummary>();
List<DeviceClick> DeviceClicks = new List<DeviceClick>();/*I need these Lists to pass
through IActionResult Index()*/
List<WebBrowser> WebBrowsers = new List<WebBrowser>();
List<CampaignCreat> CampaignCreats = new List<CampaignCreat>();
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
_logger = logger;
con.ConnectionString = CampaignReportDemo.Properties.Resources.ConnectionString;
public IActionResult Index()
FetchData();
/*I can get one list through but not multiple*/
return View(CampaignSummaries);
private void FetchData()
if(CampaignSummaries.Count > 0 && DeviceClicks.Count > 0 && WebBrowsers.Count > 0 &&
CampaignCreats.Count > 0 )
CampaignSummaries.Clear();
DeviceClicks.Clear();
WebBrowsers.Clear();
CampaignCreats.Clear();
try
con.Open();
com.Connection = con;
com.CommandText = "select FriendlyFrom, EmailsOrdered, BroadcastDate, Opens,
Clicks from CampaignResults where CampaignId = 24896;" +
";select HardwareType, count(*) as number from User_Details Group by
HardwareType;" +
"select SoftwareName, count(*) as number1 from User_Details Group By
SoftwareName;" +
"select CampaignCreative from CampaignAdCopy where CampaignId = 24896;";
dr = com.ExecuteReader();
while (dr.Read())
CampaignSummaries.Add(new CampaignSummary() FriendlyFrom =
dr["FriendlyFrom"].ToString(), BroadcastDate = dr["BroadcastDate"].ToString(),
EmailsOrdered = dr["EmailsOrdered"].ToString()
, Opens = dr["Opens"].ToString(), Clicks = dr["Clicks"].ToString());
dr.NextResult();
while (dr.Read())
DeviceClicks.Add(new DeviceClick() HardwareType =
dr["HardwareType"].ToString(), number = dr["number"].ToString() );
dr.NextResult();
while (dr.Read())
WebBrowsers.Add(new WebBrowser() SoftwareName =
dr["SoftwareName"].ToString(), number1 = dr["number1"].ToString() );
dr.NextResult();
while (dr.Read())
CampaignCreats.Add(new CampaignCreat() CampaignCreative =
dr["CampaignCreative"].ToString() );
con.Close();
catch (Exception ex)
throw ex;
public IActionResult Privacy()
return View();
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
return View(new ErrorViewModel RequestId = Activity.Current?.Id ??
HttpContext.TraceIdentifier );
索引.cshtml
@
ViewData["Title"] = "Home Page";
<section>
<table id="first">
<tr>
<th colspan="2">Campaign Report</th>
</tr>
@
if (Model != null)
foreach (var Data in Model)
<tr>
<td>Name:</td>
<td>@Data.FriendlyFrom</td>
</tr>
<tr>
<td>Date/Time:</td>
<td>@Data.BroadcastDate</td>
</tr>
<tr>
<td>Quantity:</td>
<td>@Data.EmailsOrdered</td>
</tr>
<tr>
<td>Opens:</td>
<td>@Data.Opens</td>
</tr>
<tr>
<td>Opens%:</td>
<td></td>
</tr>
<tr>
<td>Clicks:</td>
<td>@Data.Clicks</td>
</tr>
<tr>
<td>Clicks%:</td>
<td></td>
</tr>
</table>
<table>
<tr>
<th colspan="2">Device Stats By Click</th>
</tr>
@
if (Model != null)
foreach (var Data in Model)
<tr>
<td>@Data.HardwareType</td>
<td>@Data.number</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</section>
<aside valign="bottom">
<table>
<tr>
<th colspan="2">Campaign Creative</th>
@
if (Model != null)
foreach (var Data in Model)
<tr>
<td>@Data.CampaignCreative</td>
</tr>
</table>
</aside>
<article>
<table>
<tr>
<th colspan="2">Web Browser Stats By Click</th>
</tr>
@
if (Model != null)
foreach (var Data in Model)
<tr>
<td>@Data.SoftwareName @Data.number1</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</article>
【问题讨论】:
您是否考虑过定义一个包含每个 List 类型的属性的新类,然后将其传递到您的视图中?我们一般称这样一个类为“视图模型” 您的问题是您没有将正确的模型传递给您的视图。如果您需要多个模型,请创建一个新的容器类并使用它。 【参考方案1】:您应该创建一个 ViewModel 类,其中包含要在页面上显示的三个列表的属性。
在您的 index() 方法中创建一个新实例并加载数据并将它们设置为属性,然后将该视图模型对象传递给 View() 方法。
然后该类成为@Model,您可以从中访问三个列表属性,用于您的 for 循环(例如@Model.CampaignSummaries)。
不要忘记将 cshtml 页面顶部的模型类型定义更改为新的 viewModel 类类型。
【讨论】:
以上是关于在具有多个数组列表<>的 ASP.Net Core 应用程序上显示 SQL 数据 [重复]的主要内容,如果未能解决你的问题,请参考以下文章