2016-08-19 54 views
2

我需要在圖表上顯示這兩個查詢的數據。我可以一次一個條形圖地顯示數據。但我想一起顯示在同一頁面上。使用一個模型發送兩個LINQ查詢以查看

public ActionResult Daily(string label) 
{ 
    var DataContext = new BalanceDataContext(); 

    var month = label.Split('-')[0]; 
    var year = label.Split('-')[1]; 

    //Percentage of Estimated Electric Bills 
    var rdcd = new string[] { "CE", "IE" }; 
    var totalEstimated = (from a in DataContext.pujhaccds 
          join b in DataContext.pujhmtrds 
          on a.billnumber equals b.billnumber 
          where year.Equals(a.billdate.Value.Year) 
          && month.Equals(a.billdate.Value.Month) 
          && rdcd.Contains(b.reading_code) 
          group a by new {a.billdate.Value.Day} into p 
          orderby p.Key.Day ascending 
          select new Date1() { theDay2 = p.Key.Day, theCount2 = p.Count() }); 


    var totalday = (from m in DataContext.pujhaccds 
         where month.Equals(m.billdate.Value.Month) && 
         year.Equals(m.billdate.Value.Year) 
         group m by new { m.billdate.Value.Day } into p 
         orderby p.Key.Day ascending 
         select new Date1() { theDay = p.Key.Day, theCount = p.Count() }); 

    int monthInt = 0; 
    Int32.TryParse(month, out monthInt); 
    string strMonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthInt); 
    ViewBag.Message = "Bills for: " + strMonthName + year; 

    return View(new QueryView { Date2 = totalEstimated, Date1 = totalday.ToArray() }); 
} 

這裏是模型的樣子:

public class Date1 
{ 
public string Month { get; set; } 
public string Year { get; set; } 

[DataType(DataType.Date)] 
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
public DateTime? DateStart { get; set; } 
public DateTime? DateEnd { get; set; } 
public int theDay { get; set; } 
public int theDay2 { get; set; } 
public string theString { get; set; } 

[DataType(DataType.Date)] 
[DisplayFormat(DataFormatString = "{0:MMMM-yyyy}", ApplyFormatInEditMode = true)] 
public DateTime? theDate { get; set; } 
public int theCount { get; set; } 
public int theCount2 { get; set; } 
} 

我試圖創建另一種模式來保存數據,但它似乎沒有工作

public class QueryView 
{ 
    public IEnumerable<Date1> Date1 { get; set; } 
    public IEnumerable<Date1> Date2 { get; set; } 
} 

我有這在我看來最重要的,沒有錯誤信息:

@model CWebPortal.Models.QueryView 

當我有這個,我得到了錯誤:

@model IEnumerable<CWebPortal.Models.QueryView> 

這工作,但僅限於從控制器的一個查詢:

@model IEnumerable<CWebPortal.Models.Date1> 

這是我如何抓住數據在圖表查看:

jQuery(function ($) { 
     $.jqplot.config.enablePlugins = true; 
     var items = @Html.Raw(Json.Encode(Model)); 
     var ticks = []; 
     var s1 = []; 
     var s2 = []; 
     for (var i = 0; i < items.length; i++) { 
      ticks.push(items[i].theDay); 
      s1.push(items[i].theCount); 
     } 

如何將兩個查詢都放到我的視圖中?

回答

1

將您的方法合併到新的視圖模型中是絕對正確的。

public class QueryView 
{ 
    public IEnumerable<Date1> Date1 { get; set; } 
    public IEnumerable<Date1> Date2 { get; set; } 
} 

@model CWebPortal.Models.QueryView在您的看法。

只要確保你訪問你的JavaScript代碼正確的:

jQuery(function ($) { 
    $.jqplot.config.enablePlugins = true; 
    var viewModel = @Html.Raw(Json.Encode(Model)); // <--- updated 
    var items = viewModel.Date1; // <--- updated 
    var ticks = []; 
    var s1 = []; 
    var s2 = []; 
    for (var i = 0; i < items.length; i++) { 
     ticks.push(items[i].theDay); 
     s1.push(items[i].theCount); 
    }