2012-04-26 67 views
1

我有一个JSON函数返回下面的数据,这是我所期待的:数据是有,但不能显示

[{"ShowId":1,"Title":"The Forresters Axe","Date":"\/Date(1339714800000)\/","Time":{"Hours":19,"Minutes":0,"Seconds":0,"Milliseconds":0,"Ticks":684000000000,"Days":0,"TotalDays":0.79166666666666663,"TotalHours":19,"TotalMilliseconds":68400000,"TotalMinutes":1140,"TotalSeconds":68400}}] 

但是当我尝试以显示我有问题的视图页上的标题。这是视图页面代码:

<table> @foreach (var showList in Model) {<tr><td>@showList.Title</td></tr>}<.table> 

这是我一直在使用的ActionResult但是当我拨通了看法我不能得到的数据显示。我使用上面的JSON函数来检查我是否拥有正确的数据,而不是试图调用那些不存在的东西。

enter/*------------------------------------------------------- 
     BOOKING/CHECKAVAIL ACTIONRESULT CALLING CHECKAVAIL VIEW 
     Select information from Run table where the id == ShowId 

    ------------------------------------------------------*/ 
    public ActionResult CheckAvail(int id) 
    { 
     var showList = from r in db.Runs 
         join s in db.Shows on r.ShowId equals s.ShowId 
         where r.ShowId == id 
         select new 
        { 
         ShowId = r.ShowId, 
         Title = s.Title, 
         Date = r.Date, 
         Time = r.Time 
        }; 
     return View(showList); 
    } 

我得到的错误是:

RuntimeBinderException是通过代码未处理

“对象”不包含标题

回答

0

的定义有了new { ... }要创建一个anonymous type其表达基本上不支持通过View方法作为模型。

有几个解决方法:

但最简单的解决方案是创建一个视图模型,将保存数据:

public class CheckAvailViewModel 
{ 
    public int ShowId { get; set; } 
    public string Title { get; set; } 
    public DateTime Date { get; set; } 
    public DateTime Time { get; set; } 
} 

而且从您的查询中返回:

var showList = from r in db.Runs 
        // ... 
        select new CheckAvailViewModel 
        { 
         ShowId = r.ShowId, 
         Title = s.Title, 
         Date = r.Date, 
         Time = r.Time 
        }; 
return View(showList.ToArray()); 
相关问题