在具有多个阵列列表的 ASP.Net 核心应用程序上显示 SQL 数据<>



我找到了一个教程,介绍如何通过"public IActionResult Index(("但我需要多个List,其中包含通过我的index.cshtml页面传递的多个sql查询语句中的数据。我正在使用ASP。Net Core MVC。我有多个模型,我将展示和家庭控制器。我也可以显示cshtml。我试图通过一个List<gt;但是它会打印出多个空的第一个查询语句。

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; }
}
}

Web浏览器.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>

您应该创建一个ViewModel类,该类具有要在页面上显示的三个列表的属性。

在index((方法中,创建一个新的实例,加载数据并将其设置为属性,然后将该视图模型对象传递给View((方法。

然后,该类成为@Model,您可以从中访问for循环的三个列表属性(例如,@Model.CampaignSemmaries(。

不要忘记将cshtml页面顶部的模型类型定义更改为新的viewModel类类型。

相关内容

最新更新