0

I've been using the ExpandoObject() workaround for anonymous types that has been discussed on this site. I am running into a problem though as my view won't compile. I have used the following extension:

    public static ExpandoObject ToExpando(this object anonymousObject)
    {
        IDictionary<string, object> anonymousDictionary = new RouteValueDictionary(anonymousObject);
        IDictionary<string, object> expando = new ExpandoObject();
        foreach (var item in anonymousDictionary)
            expando.Add(item);
        return (ExpandoObject)expando;
    }

...which allows me to do the following:

Controller:

        var latestDiscussions = repository.Messages
            .OrderByDescending(m => m.DateCreated)
            .GroupBy(m => m.Discussion.DiscussionId)
            .Take(10)
            .Join(repository.Discussions,
                m => m.Key,
                d => d.DiscussionId,
                (m, d) => new
                {
                    Id = m.Key,
                    Title = d.Title,
                    Guid = d.Guid,
                    UrlTitle = d.UrlTitle,
                    ViewCount = d.ViewCount
                }).ToExpando();

View:

@foreach (var discussion in Model.Latest)
{
    <div>
        @Html.RouteLink(discussion.Title, "DisplayDiscussion", new { guid = discussion.Guid, urlTitle = discussion.UrlTitle, action = "Display", controller = "Discussion" })
    </div>
}

My model looks like this:

public class IndexModel
{
    public IQueryable<Discussion> MostPopular { get; set; }
    public ExpandoObject Latest { get; set; }
}

However, I am getting a compilation error saying that discussion.Title:

Compiler Error Message: CS1061: 'System.Collections.Generic.KeyValuePair' does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'System.Collections.Generic.KeyValuePair' could be found (are you missing a using directive or an assembly reference?)

What am I missing?

Thanks in advance!

Mark

serlingpa
  • 12,024
  • 24
  • 80
  • 130

0 Answers0