简体   繁体   English

'object'不包含'Name'的定义

[英]'object' does not contain a definition for 'Name'

I'm using two DataContext objects to return seperate AsQueriable() datasets then joining the two using linq. 我使用两个DataContext对象返回单独的AsQueriable()数据集,然后使用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. 在调试会话期间,我可以清楚地看到父模型和foreach循环中的每个“项”都具有可见/可访问的所有数据和键。 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. stackoverflow.com上与此问题相关的许多其他q&a都没有解决我的问题,因此会欣赏一双新鲜的眼睛并希望能够解决这个问题。

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: VSExpress的错误屏幕: 例外截图

Any guidance on a solution would be greatly appreciated. 任何有关解决方案的指导都将不胜感激。 Thank you! 谢谢!

C C

I would suggest you create a single model to encapsulate both IEnumerable objects, eg 我建议你创建一个单独的模型来封装IEnumerable对象,例如

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, eg 然后在您的模型中,您可以键入 - 安全地引用每个对象,例如

<% foreach (var t in Model.Galleries) ...

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. 链接问题涵盖了这些主题。

Try changing your for loop to: 尝试将for循环更改为:

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

The use of var is causing the type of t to be inferred as object . var的使用导致t的类型被推断为object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM