5

I'm using two DataContext objects to return seperate AsQueriable() datasets then joining the two using linq. The data construction works perfectly however when I pass that combined dataset to the view, I'm getting the error 'object' does not contain a definition for 'Name'.

During a debug session I can clearly see that both the parent Model and each 'item' in the foreach loop has all the data and keys visible/accessible. I'm very confused.

Many of the other q&a's on stackoverflow.com that match this problem don't solve my issue and as a result would appreciate a fresh set of eyes and hopefully a solution to this problem.

Many thanks! - code time:

The data construction

        public ActionResult SplashImages()
        {
            var g = (from i in GetGallerySplash() join o in GetFestivals() on i.Festival equals o.ID orderby i.Rating descending select new {i.Photo, i.OwnedBy, i.Rating, o.Name });
            Response.ContentType = "text/xml";
            return View(g);
        }

        private IEnumerable<Gallery> GetGallerySplash()
        {
            GallerysDataContext gdc = new GallerysDataContext();
            return (from i in gdc.Galleries orderby i.Rating descending select i).Take(15).AsQueryable();
        }

        private IEnumerable<Festival> GetFestivals()
        {
            FestivalsDataContext fdc = new FestivalsDataContext();
            return (from i in fdc.Festivals select i).AsQueryable();
        }

VSExpress's error screen: Exception screenshot

Any guidance on a solution would be greatly appreciated. Thank you!

C

Chris M
  • 182
  • 2
  • 4
  • 17
  • possible duplicate of [Accessing C# Anonymous Type Objects](http://stackoverflow.com/questions/713521/accessing-c-sharp-anonymous-type-objects) – nawfal Jun 28 '14 at 12:49

3 Answers3

4

I would suggest you create a single model to encapsulate both IEnumerable objects, e.g.

public class GalleryModel
{
    public IEnumerable<Gallery> Galleries { get; set }
    public IEnumerable<Festivals> Festivals { get; set; }
}

Then strongly type your view to match the model

...Inherits="System.Web.Mvc.ViewPage<GalleryModel>"

Then in your model, you can type-safely refer to each object, e.g.

<% foreach (var t in Model.Galleries) ...
tobias86
  • 4,979
  • 1
  • 21
  • 30
  • Thanks Tobias86, I was just about finishing this exact approach as I saw your answer. You are exactly right of course. I did make a new Model for the combined datasets and now the page is working perfectly. Many thanks! – Chris M Apr 21 '11 at 10:09
  • 1
    Glad to be of help. We've had to deal with similar scenarios, so I've been where you've been :) – tobias86 Apr 21 '11 at 10:11
  • 1
    I believe this is called the **MVVM** (`Model View ViewModel`) design pattern. Basically you add another layer between your Model and View - a ViewModel - allowing you to better encapsulate your model into a more view-friendly format. Many people who use the MVVM pattern add another folder called `ViewModels` to their project, emulating the usage of the Model, View, and Controller folders, and place all their ViewModel classes within. – Chad Levy Sep 15 '11 at 10:09
1

You're returning anonymous type to display in view, hence the problems. Take a look at those questions:

You can either wrap your anonymous type in actual class and use strongly typed view, or play with some dynamic magic. Linked questions cover those topics.

Community
  • 1
  • 1
k.m
  • 30,794
  • 10
  • 62
  • 86
-1

Try changing your for loop to:

<% foreach (dynamic t in Model) { %>

The use of var is causing the type of t to be inferred as object.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • 2
    if `Model` is not `IEnumerable` i would rather go for a static typed version and introduce a poco ... `dynamic` is basically pure reflection! –  Apr 21 '11 at 09:54
  • Hi Matthew, I have applied that change unfortunately I still get the same error. – Chris M Apr 21 '11 at 09:54
  • the issue is that the anonymous type will not escape the scope of the action method and the view only knows the Model as type object. – Jimmy Bosse Feb 22 '13 at 21:37