开发者

Trouble returning linq query result into View

开发者 https://www.devze.com 2023-02-17 12:00 出处:网络
This is my controller code: using (var db = new MatchGamingEntities()) { var MyMatches = from m in db.Matches

This is my controller code:

 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();
        }

Here is my View Code:

@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>

None of my @item's work, I get an error saying that they are unknown. My database has records and is setup properly. The common column they share is MatchTypeId. I dont know how to check if the query is going through either. Did I set this join up wrong?

UPDATE:

This is the error Message:

'object' does not contain a definition for 'MatchTypeName'

Here is the exception

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开发者_C百科.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: 


This is because your LINQ uses an anonymous type as the final result (The select new{} bit). In the view, because ViewBag is dynamic, it is able to resolve that there is a "MyMatches" property, however there is no way to resolve the type of what is stored in the collection beyond just an object.

One option to solve this would be to create a new class, ie:

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

Then instead of select new {}, you could select new MatchInfo {} and initialize the properties. In the iteration step in the View, you would change it to:

 @foreach (MatchInfo item in ViewBag.MyMatches) {

 }

Now the view has a strong type to work with, and the properties (MatchTypeName, MatchTitle, etc) will resolve.


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

should be

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

精彩评论

暂无评论...
验证码 换一张
取 消