2012-02-01 79 views
0

對於mvc和im,我寫了一份報告,根據國家和零售商獲取數據 - 如果國家沒有零售商,則空表格顯示0 s在列中。MVC:如果沒有價值,我該如何隱藏表格

我希望它做的是跳躍到下一個國家,並在所有

我已經使用

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

查看:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    <%: ViewData["title"] %> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <h1 title="<%: ViewData["title"] %>"> 
     <%: ViewData["title"]%> 
    </h1> 
    <% for (int cumLeadsI = 1; cumLeadsI <= Convert.ToInt32(ViewData["numberOfCountries"]); cumLeadsI++) 
     { %> 

    <h2 title="<%: ViewData["country" + cumLeadsI + "Name"] %>"> 
     <%: ViewData["country" + cumLeadsI + "Name"] %></h2> 
    <table style="font-size: 80%; border-collapse: collapse; margin-left: 10px;"> 
     <thead> 
      <tr style="background-color: #f3f3f3; font-weight: bold;"> 
       <td> 
        &nbsp; Week&nbsp; 
       </td> 
       <td> 
        &nbsp; Week number&nbsp; 
       </td> 
       <% for (int numRetailer = 1; numRetailer <= Convert.ToInt32(ViewData["retailersForCountry" + cumLeadsI]); numRetailer++) 
        { %> 
       <td> 
        &nbsp; 
        <%: ViewData["retailer" + numRetailer + "forCountry" + cumLeadsI + "Name"]%>&nbsp; 
       </td> 
       <% } %> 
       <td> 
        &nbsp; Total&nbsp; 
       </td> 
       <td> 
        &nbsp; Cummulative Total&nbsp; 
       </td> 
      </tr> 
     </thead> 
     <tbody> 
      <% for (int cumLeadsJ = 1; cumLeadsJ <= Convert.ToInt32(ViewData["numberOfWeeks"]); cumLeadsJ++) 
       {%> 
      <% if (cumLeadsJ % 2 != 0) 
       { %> 
      <tr> 
       <% } 
       else 
       { %> 
       <tr style="background-color: #f3f3f3;"> 
        <% } %> 
        <td> 
         <%: ViewData["weekRange" + cumLeadsI + "Range" + cumLeadsJ] %> 
        </td> 
        <td> 
         <%: ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ] %> 
        </td> 
        <% for (int cumLeadsK = 1; cumLeadsK <= Convert.ToInt32(ViewData["retailersForCountry" + cumLeadsI]); cumLeadsK++) 
         { %> 
        <td> 
         <%: ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ + "retailer" + cumLeadsK] %> 
        </td> 
        <% } %> 
        <td> 
         <%: ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ + "totalForWeek"] %> 
        </td> 
        <td> 
         <%: ViewData["weekNumber" + cumLeadsI + "Range" + cumLeadsJ + "cumulativeForWeek"] %> 
        </td> 
       </tr> 
       <% } %> 
     </tbody> 
     <tfoot> 
      <tr style="border-top: 1px solid black; font-weight: bold; background-color: #f3f3f3; "> 
       <td style="text-align:left;"> 
        Total 
       </td> 
       <td> 
        &nbsp; 
       </td> 
       <% for (int cumLeadsK = 1; cumLeadsK <= Convert.ToInt32(ViewData["retailersForCountry" + cumLeadsI]); cumLeadsK++) 
        {%> 
       <td> 
        <%: ViewData["retailerTotalForCountry" + cumLeadsI + "RetailerTotal" + cumLeadsK] %> 
       </td> 
       <% 
        }%> 
       <td> 
        &nbsp; 
       </td> 
       <td> 
        <%: ViewData["crossTotal" + cumLeadsI]%> 
       </td> 
      </tr> 
      <tr style="font-weight: bold; background-color: #f3f3f3; border-bottom: 1px solid black; "> 
       <td style="text-align:left;"> 
        Reseller Share 
       </td> 
       <td> 
        &nbsp; 
       </td> 
       <% for (int cumLeadsK = 1; cumLeadsK <= Convert.ToInt32(ViewData["retailersForCountry" + cumLeadsI]); cumLeadsK++) 
        {%> 
       <td> 
        &nbsp; 
        <%: Convert.ToDouble(ViewData["percentageForRetailer" + cumLeadsI + "RetailerPercentage" + cumLeadsK]).ToString("F") %>%&nbsp; 
       </td> 
       <% 
        }%> 
       <td> 
        &nbsp; 
       </td> 
       <td> 
        &nbsp; 
       </td> 
      </tr> 
     </tfoot> 
    </table> 

感謝

+1

對於記錄安娜,它被認爲是非常糟糕的形式有這麼長的行動方法。您通常希望將處理放入模型或服務類中。我個人的經驗法則是在操作方法中不超過5行代碼。另外,你應該很少使用ViewData - 如果你剛剛創建了一個模型類,那麼你的很多邏輯將會變得更加簡單和清晰。 – 2012-02-01 15:49:50

+0

謝謝我會查看那 – anna 2012-02-01 16:01:10

+0

是否有一個特定的原因,你把所有這些數據填入查看數據?您將它用作大容量存儲容器,而不是將模型或視圖模型與所有數據一起傳遞。 – 2012-02-01 16:14:26

回答

1

簡短的回答:檢查組的長度,打印出表之前。 你的情況:

<% if(Convert.ToInt32(ViewData["retailersForCountry" + cumLeadsI]) > 0) {%> 
    <table> 
    ..... 

朗的答案,你會發現這樣的回答自己,如果你清理你的風格問題。例如,你應該從來沒有發現自己在視圖中使用Convert類,你應該很少使用ViewData結構(它對於像'選項來填充下拉列表'這樣的東西是有用的,但真的不應該用於其他許多)。你應該爲你需要的值創建一個非常簡單的類,並通過Model屬性使用它(你應該是同一類型的)。

此外,大多數人已經開始使用Razor視圖模板,而不是您正在使用的(webforms)。這在某種程度上是個人喜好,但它足夠相似,應該花大約6分鐘的時間學習,你會發現幾乎所有當前使用Razor編寫的例子。 Here is a quick equivalence table

我意識到上述聽起來很刺耳,但你應該可以隨意使用StackOverflow作爲資源 - 這裏的人會幫助你,而且你的編碼會變得更容易。

+0

謝謝 - 我一定會研究剃刀 – anna 2012-02-01 16:12:35

相關問題