2012-04-26 74 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()); 
相關問題