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>
4

2 に答える 2

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
        };
于 2012-05-22T19:09:33.033 に答える
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);
    }
于 2012-05-22T19:10:37.207 に答える