调试 - 将数据从控制器传递到视图

时间:2012-05-22 19:05:11

标签: c# asp.net-mvc asp.net-mvc-3 model-view-controller

我有一个集合:

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

我正试图建立一个'spot'的视图模型。

但是当我编译时,我收到以下错误:

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>

2 个答案:

答案 0 :(得分: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
        };

答案 1 :(得分: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);
    }