2011-03-16 30 views
0

这是我的控制器代码:故障返回LINQ查询结果到视图

using (var db = new MatchGamingEntities()) 
     { 
      var MyMatches = from m in db.Matches 
          join n in db.MatchTypes on m.MatchTypeId equals n.MatchTypeId 
          select new{ 
           MatchTypeName = n.MatchTypeName, 
           MatchTitle = m.MatchTitle, 
           Wager = m.Wager 
          }; 
      ViewBag.MyMatches = MyMatches.ToList(); 



      return View(); 
     } 

这是我查看代码:

我@项目的工作
@foreach (var item in ViewBag.MyMatches) { 
    <tr> 
     <td> 
      @item.MatchTypeName 
     </td> 
     <td> 
     <a href="Detials/@item.MatchTypeId">@item.MatchTitle</a> 

     </td> 


     <td> 
      @String.Format("{0:F}", item.Wager) 
     </td> 

    </tr> 

无,我得到一个错误,说他们是未知的。我的数据库有记录并且设置正确。它们共享的公共列是MatchTypeId。我不知道如何检查查询是否通过。我是否设置了错误?

UPDATE:

这是错误消息:

'对象' 中不包含关于 'MatchTypeName'

这里是例外

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled by user code 
    Message='object' does not contain a definition for 'MatchTypeName' 
    Source=Anonymously Hosted DynamicMethods Assembly 
    StackTrace: 
     at CallSite.Target(Closure , CallSite , Object) 
     at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) 
     at ASP._Page_Views_Matches_Index_cshtml.Execute() in c:\Users\Anthony\Documents\Visual Studio 2010\Projects\MatchGaming\MatchGaming\Views\Matches\Index.cshtml:line 31 
     at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
     at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
     at System.Web.WebPages.StartPage.RunPage() 
     at System.Web.WebPages.StartPage.ExecutePageHierarchy() 
     at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
     at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) 
     at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) 
     at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 
     at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) 
    InnerException: 
+1

是什么错误说? – SLaks 2011-03-16 21:23:24

+0

@SLaks更新为异常,我记得你在其他问题上发帖,谢谢你的帮助人 – anthonypliu 2011-03-16 21:28:05

回答

3

这是因为定义你的LINQ使用匿名类型作为最终结果(Select new {} bit)。在该视图中,因为ViewBag是动态的,所以它能够解决存在“MyMatches”属性的问题,但无法解决存储在集合中的内容的类型,而不仅仅是一个对象。要解决这个

一种选择是创建一个新的类,即:

public class MatchInfo 
{ 
    public string MatchTypeName { get; set; }  
    public string MatchTitle { get; set; } 
    public decimal Wager { get; set; } 
} 

然后,而不是选择新的{},您可以选择新的MatchInfo {}和初始化属性。在视图迭代步骤中,您将其更改为:

@foreach (MatchInfo item in ViewBag.MyMatches) { 

} 

现在的观点具有很强的类型的工作,和属性(MatchTypeName,MatchTitle等)将解决。

0
<a href="Detials/@item.MatchTypeId">@item.MatchTitle</a> 

应该

@Html.ActionLink("name link", "action", new { /* id=item.PrimaryKey */ })