我正在使用两个 DataContext 对象返回单独的 AsQueriable() 数据集,然后使用 linq 将这两个数据集连接起来。数据构建工作完美,但是当我将该组合数据集传递给 View 时,出现错误“对象”不包含“名称”的定义。
在调试 session 期间,我可以清楚地看到父模型和 foreach 循环中的每个“项目”都具有可见/可访问的所有数据和键。我很困惑。
stackoverflow.com 上与此问题相匹配的许多其他问答都没有解决我的问题,因此希望有新的眼光,并希望能找到解决此问题的方法。
非常感谢! - 代码时间:
数据构建
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 的错误屏幕:
如能提供任何有关解决方案的指导,我们将不胜感激。谢谢!
C
最佳答案
我建议您创建一个模型来封装两个 IEnumerable 对象,例如
public class GalleryModel
{
public IEnumerable<Gallery> Galleries { get; set }
public IEnumerable<Festivals> Festivals { get; set; }
}
然后强类型你的 View 以匹配模型
...Inherits="System.Web.Mvc.ViewPage<GalleryModel>"
然后在您的模型中,您可以类型安全地引用每个对象,例如
<% foreach (var t in Model.Galleries) ...
关于c# - 'object' 不包含 'Name' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5742339/