C# MVC:如果表没有值,我如何隐藏它

C# MVC:如果表没有值,我如何隐藏它,c#,asp.net-mvc,C#,Asp.net Mvc,您好,我是mvc的新手,我正在编写一份报告,该报告按国家和零售商获取数据-如果国家没有零售商,则会显示一个空表,列中显示0 我想要它做的是跳到下一个国家,而不显示该国的表格 我用过 ViewData["retailersForCountry"] = retailersForCountry; if (retailersForCountry == 0) { continue; } 但是表的页眉和页脚仍然显示在屏幕上——我怎么能完全隐藏它,然后只显示下一个带有值的表呢 控制器代码: case

您好,我是mvc的新手,我正在编写一份报告,该报告按国家和零售商获取数据-如果国家没有零售商,则会显示一个空表,列中显示0

我想要它做的是跳到下一个国家,而不显示该国的表格

我用过

ViewData["retailersForCountry"] = retailersForCountry;
if (retailersForCountry == 0)
{
    continue;
}
但是表的页眉和页脚仍然显示在屏幕上——我怎么能完全隐藏它,然后只显示下一个带有值的表呢

控制器代码:

case "CUMLEADS":
    // Report Title
    ViewData["title"] = reportRequest.Title;

    // Store the ReportRequest Id
    ViewData["reportRequestId"] = reportRequest.Id;

    // number Of COuntries so that we can set our outermost loop
    var numberOfCountriesCumLeads = Convert.ToInt32(_reportValueRepository.GetReportPair(reportRequest.Id, "numberOfCountries").Value);
    ViewData["numberOfCountries"] = numberOfCountriesCumLeads;

    // Our outermost country loop

    for (int cumLeadsI = 1; cumLeadsI <= numberOfCountriesCumLeads; cumLeadsI++)
    {
        int cumulativeTotalForCountry = 0;

        // Get the number of Retailers for this country -check that the amount of retailers is not 0 if it is then skip to next country
        var retailersForCountry = Convert.ToInt32(_reportValueRepository.GetReportPair(reportRequest.Id, "retailersForCountry" + cumLeadsI).Value);
        //TODO
        //If retailerForCountry = 0 then go to next country - need to remove the header footer
        ViewData["retailersForCountry"] = retailersForCountry;
        if (retailersForCountry == 0)
        {
            continue;
        }
        ViewData["retailersForCountry" + cumLeadsI] = retailersForCountry;

        var totalRetailPerCountry = new int[retailersForCountry + 1];

        for (int numRetailer = 1; numRetailer <= retailersForCountry; numRetailer++)
        {
            ViewData["retailer" + numRetailer + "forCountry" + cumLeadsI + "Name"] = _reportValueRepository.GetReportPair(reportRequest.Id, "retailer" + numRetailer + "forCountry" + cumLeadsI + "Name").Value;
        }

        // get the country name
        ViewData["country" + cumLeadsI + "Name"] = _reportValueRepository.GetReportPair(reportRequest.Id, "country" + cumLeadsI + "Name").Value;

        // We need to go through the dates in order

        // Create a loop that will go through the range of dates that we have in the request
        var myStartDate = reportRequest.StartDate;

        // I need to store in the view the total number of weeks that we are going to do, and then for each ith week store the week number and the date range for that week
        var actualEndDate = reportRequest.EndDate;
        TimeSpan timespan = actualEndDate.Subtract(myStartDate);
        int numberOfWeeks = timespan.Days / 7;
        ViewData["numberOfWeeks" + cumLeadsI] = numberOfWeeks;
        int cumLeadsJ = 1;

        while (myStartDate.CompareTo(reportRequest.EndDate) < 0)
        {
            int totalForWeek = 0;
            var myEndDate = myStartDate.AddDays(6);
            if (myEndDate.CompareTo(reportRequest.EndDate) > 0)
            {
                myEndDate = reportRequest.EndDate;
            }

            // Store the Range of the data for display
            ViewData["weekRange" + cumLeadsI + "Range" + cumLeadsJ] = myStartDate.ToShortDateString() + "-" + myEndDate.ToShortDateString();

            // Go through each of the retailers for this date and this country to do 1 row
            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
            Calendar cal = dfi.Calendar;
            ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ] = cal.GetWeekOfYear(myStartDate, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);

            // I need to loop thougth each of the retailers for this country and get the values for this myStartDate to put into the view
            for (int cumLeadsK = 1; cumLeadsK <= retailersForCountry; cumLeadsK++)
            {
                var retailerForWeek = Convert.ToInt32(_reportValueRepository.GetReportPair(reportRequest.Id, "retailer" + cumLeadsK + "forCountry" + cumLeadsI + myStartDate + "Count").Value);
                ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ + "retailer" + cumLeadsK] = retailerForWeek;
                totalForWeek += retailerForWeek;
                totalRetailPerCountry[cumLeadsK] += retailerForWeek;
            }

            ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ + "totalForWeek"] = totalForWeek;
            cumulativeTotalForCountry += totalForWeek;
            ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ + "cumulativeForWeek"] = cumulativeTotalForCountry;

            // Move onto the next week
            myStartDate = myStartDate.AddDays(7);
            cumLeadsJ++;
        }

        int crossTotal = 0;
        // I need to loop though each of the retailers for this country and get the values for this myStartDate to put into the view
        for (int cumLeadsK = 1; cumLeadsK <= retailersForCountry; cumLeadsK++)
        {
            crossTotal += totalRetailPerCountry[cumLeadsK];
            ViewData["retailerTotalForCountry" + cumLeadsI + "RetailerTotal" + cumLeadsK] = totalRetailPerCountry[cumLeadsK];

        }
        ViewData["crossTotal" + cumLeadsI] = crossTotal;

        for (int cumLeadsK = 1; cumLeadsK <= retailersForCountry; cumLeadsK++)
        {
            ViewData["percentageForRetailer" + cumLeadsI + "RetailerPercentage" + cumLeadsK] = Convert.ToDouble(totalRetailPerCountry[cumLeadsK]) / Convert.ToDouble(crossTotal) * 100.0;
        }

        ViewData["numberOfWeeks"] = cumLeadsJ;
    }
    return View(report.Code);
案例“CUMLEADS”:
//报告标题
ViewData[“title”]=reportRequest.title;
//存储ReportRequestID
ViewData[“reportRequestId”]=reportRequest.Id;
//国家的数量,以便我们可以设置最外层的环路
var numberOfCountriesCumLeads=Convert.ToInt32(_reportValueRepository.GetReportPair(reportRequest.Id,“numberOfCountries”).Value);
ViewData[“numberOfCountries”]=NumberOfCountriesUMLEADS;
//我们最外面的国家环路

对于(int cumLeadsI=1;cumLeadsI简短回答:在打印表格之前检查集合的长度。 就你而言:

<% if(Convert.ToInt32(ViewData["retailersForCountry" + cumLeadsI]) > 0 ) {%>
  <table>
   .....
0){%>
.....
长答案,如果你整理一下你的风格,你会发现像这样的问题会自己回答。例如,你永远不会发现自己在视图中使用Convert类,你应该很少使用ViewData结构(它对于“填充下拉列表的选项”之类的事情很有用,但实际上不应该用于其他很多事情)。您应该为所需的值创建一个非常简单的类,并通过Model属性使用它(您应该具有相同的类型)

此外,大多数人已经开始使用Razor视图模板,而不是您正在使用的(webforms)。这在某种程度上是个人偏好,但它非常相似,需要大约6分钟的时间来学习,您将发现几乎所有当前的示例都是用Razor编写的


我意识到上面的说法听起来很苛刻,但你可以随意使用StackOverflow作为一种资源——这里的人会提供帮助,你的编码也会变得更简单。

请注意,anna,动作方法这么长被认为是非常糟糕的形式。你通常希望将处理放入模型或服务类中。我个人的规则是thumb在action方法中的代码行数不超过5行。此外,您应该很少使用ViewData—如果您只创建一个模型类,这里的许多逻辑将更简单、更清晰。是否有特定的原因将所有这些数据塞进View数据?您将其用作一个大的存储容器,而不是传递ar使用所有数据创建模型或视图模型。
<% if(Convert.ToInt32(ViewData["retailersForCountry" + cumLeadsI]) > 0 ) {%>
  <table>
   .....