C# 无法将linq查询结果返回到视图中

C# 无法将linq查询结果返回到视图中,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,这是我的控制器代码: 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{

这是我的控制器代码:

 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>
@foreach(ViewBag.MyMatches中的变量项){
@item.MatchTypeName
@String.Format(“{0:F}”,item.Wager)
我的@item没有工作,我收到一个错误,说它们是未知的。我的数据库有记录,并且设置正确。它们共享的公共列是MatchTypeId。我不知道如何检查查询是否正在进行。我是否设置了此连接错误

更新:

这是错误消息:

“对象”不包含“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: 
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException未由用户代码处理
Message='object'不包含'MatchTypeName'的定义
Source=匿名托管的DynamicMethods程序集
堆栈跟踪:
目标(闭包、调用站点、对象)
在System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](调用站点,T0 arg0)
在c:\Users\Anthony\Documents\Visual Studio 2010\Projects\MatchGaming\MatchGaming\Views\Matches\Index中的ASP.\Page\Views\Matches\Index\cshtml.Execute()中
在System.Web.WebPages.WebPageBase.ExecutePageHierarchy()中
在System.Web.Mvc.WebViewPage.ExecutePageHierarchy()中
在System.Web.WebPages.StartPage.RunPage()中
在System.Web.WebPages.StartPage.ExecutePageHierarchy()中
位于System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext、TextWriter writer、WebPageRenderingBase起始页)
位于System.Web.Mvc.RazorView.RenderView(ViewContext-ViewContext、TextWriter-writer、对象实例)
位于System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext、ViewContext、TextWriter)
在System.Web.Mvc.ViewResultBase.ExecuteSult(ControllerContext上下文)中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext ControllerContext,ActionResult ActionResult)
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass1C.b_uuu19()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
内部异常:

这是因为LINQ使用匿名类型作为最终结果(select new{}位)。在视图中,由于ViewBag是动态的,它能够解析存在“MyMatches”属性,但是除了对象之外,无法解析存储在集合中的类型

解决这个问题的一个方法是创建一个新类,即:

 public class MatchInfo 
 {
     public string MatchTypeName { get; set; }     
     public string MatchTitle { get; set; }
     public decimal Wager { get; set; }
 }
然后,您可以选择new MatchInfo{}并初始化属性,而不是选择new{},在视图的迭代步骤中,您可以将其更改为:

 @foreach (MatchInfo item in ViewBag.MyMatches) {

 }

现在,视图有一个强类型可处理,属性(MatchTypeName、MatchTitle等)将被解析。

这是因为LINQ使用匿名类型作为最终结果(选择新{}位)。在视图中,由于ViewBag是动态的,它能够解析存在“MyMatches”的问题属性,但是除了对象之外,无法解析存储在集合中的内容的类型

<a href="Detials/@item.MatchTypeId">@item.MatchTitle</a>
解决这个问题的一个方法是创建一个新类,即:

 public class MatchInfo 
 {
     public string MatchTypeName { get; set; }     
     public string MatchTitle { get; set; }
     public decimal Wager { get; set; }
 }
然后,您可以选择new MatchInfo{}并初始化属性,而不是选择new{},在视图的迭代步骤中,您可以将其更改为:

 @foreach (MatchInfo item in ViewBag.MyMatches) {

 }

现在视图有了一个强类型和属性(MatchTypeName、MatchTitle等)将解决。

错误说明是什么?@SLaks也有例外更新,我记得你在我的其他问题上发表了帖子,非常感谢你的帮助。错误说明是什么?@SLaks也有例外更新,我记得你在我的其他问题上发表了帖子,非常感谢你的帮助
<a href="Detials/@item.MatchTypeId">@item.MatchTitle</a>