2012-05-22 49 views
0

我有一個集合:調試 - 傳遞數據,從控制器查看

  var results = 
      from result in xml.Descendants(ns + "GetQuoteResult") 
      select new SpotPriceModel 
      { 
       Type = result.Element(ns + "Type").Value, 
       Currency = result.Element(ns + "Currency").Value, 
       Date = result.Element(ns + "Date").Value, 
       Time = result.Element(ns + "Time").Value, 
       Rate = (decimal)result.Element(ns + "Rate"), 
       Bid = (decimal)result.Element(ns + "Bid"), 
       BidTime = result.Element(ns + "BidTime").Value, 
       ExpTime = result.Element(ns + "ExpTime").Value, 
       DisplayTime = result.Element(ns + "DisplayTime").Value, 
       DisplayDate = result.Element(ns + "DisplayDate").Value, 
       Ask = (decimal)result.Element(ns + "Ask"), 
       AskTime = result.Element(ns + "AskTime").Value 
      }; 

     var spot = results.First(); 

     return View(spot); 
    } 

而且我已經創建了一個集合的模式:

public class SpotPriceModel 
{ 
    public string Type { get; set; } 
    public string Currency { get; set; } 
    public DateTime Date { get; set; } 
    public DateTime Time { get; set; } 
    public decimal Rate { get; set; } 
    public decimal Bid { get; set; } 
    public DateTime BidTime { get; set; } 
    public DateTime ExpTime { get; set; } 
    public DateTime DisplayTime { get; set; } 
    public DateTime DisplayDate { get; set; } 
    public decimal Ask { get; set; } 
    public DateTime AskTime { get; set; } 
} 

我試圖支架「視圖模型點'。

但是,當我編譯,我得到以下錯誤:

The model item passed into the dictionary is of type 'BD.Models.SpotPriceModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[BD.Models.SpotPriceModel]'. 

視圖期待下面的模型類型:

@model IEnumerable<BD.Models.SpotPriceModel> 
+0

在模型中聲明的是什麼類型的視圖中? – Jesse

回答

2

您還沒有指定的類型在你的Linq查詢和所以創建了一個匿名類型而不是SpotPriceModel。

  select new SpotPriceModel 
     { 
      Type = result.Element(ns + "Type").Value, 
      Currency = result.Element(ns + "Currency").Value, 
      Date = result.Element(ns + "Date").Value, 
      Time = result.Element(ns + "Time").Value, 
      Rate = (decimal)result.Element(ns + "Rate"), 
      Bid = (decimal)result.Element(ns + "Bid"), 
      BidTime = result.Element(ns + "BidTime").Value, 
      ExpTime = result.Element(ns + "ExpTime").Value, 
      DisplayTime = result.Element(ns + "DisplayTime").Value, 
      DisplayDate = result.Element(ns + "DisplayDate").Value, 
      Ask = (decimal)result.Element(ns + "Ask"), 
      AskTime = result.Element(ns + "AskTime").Value 
     }; 
+0

感謝您的回覆。我現在有一個新問題...我已經更新了這個問題。 – Gravy

+0

完美,有效。我的第二個問題是我搭建了一個列表而不是細節視圖。 – Gravy

1

,而不是使用匿名類型嘗試

IEnumerable<SpotPriceModel> results =(from result in xml.Descendants(ns + "GetQuoteResult") 
       select new SpotPriceModel  
      { 
       Type = result.Element(ns + "Type").Value, 
       Currency = result.Element(ns + "Currency").Value, 
       Date = result.Element(ns + "Date").Value, 
       Time = result.Element(ns + "Time").Value, 
       Rate = (decimal)result.Element(ns + "Rate"), 
       Bid = (decimal)result.Element(ns + "Bid"), 
       BidTime = result.Element(ns + "BidTime").Value, 
       ExpTime = result.Element(ns + "ExpTime").Value, 
       DisplayTime = result.Element(ns + "DisplayTime").Value, 
       DisplayDate = result.Element(ns + "DisplayDate").Value, 
       Ask = (decimal)result.Element(ns + "Ask"), 
       AskTime = result.Element(ns + "AskTime").Value 
      }).AsEnumerable();  
     //var spot = results.First();  
     return View(results); 
    } 
+0

感謝您的迴應。我現在有一個新問題...我已經更新了這個問題。 – Gravy

+0

看到編輯... – Rafay

+0

完美,它工作。我的第二個問題是我搭建了一個列表而不是細節視圖。 – Gravy

相關問題