0

I found a tutorial on how to pass one array list with sql data in it through "public IActionResult Index()" but I need multiple Lists with data from multiple sql query statements passed through my index.cshtml page. I am using ASP.Net Core MVC. I have multiple Models which I will show and the HomeController. I can show the cshtml too. I tried to pass all the query statements through one List<> but it would print out multiple of the first query statement empty.

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

Index.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>
2
  • 2
    Have you considered defining a new class that contains properties of each of your List types, and then passing that into your view? We generally call such a class a "view model" Commented Jun 22, 2021 at 16:34
  • 1
    Your problem is that you are not passing the correct model to your view. If you need more than one model, create a new container class and use that instead. Commented Jun 22, 2021 at 16:50

1 Answer 1

0

You should create a ViewModel class with properties for the three lists that you want to show on your page.

In your index() method create a new instance of that and load up the data and set them to the properties, and then pass that viewmodel object to the View() method.

That class then becomes the @Model and you access the three list properties from it for each for your for loops (@Model.CampaignSummaries for example).

Don't forget to change the model type defintion at the top of the cshtml page to you new viewModel class type.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.