我有一个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'object'不包含Title
的定义答案 0 :(得分:0)
使用表达式new { ... }
,您创建的anonymous type基本上不支持将View
方法作为模型传递。
有一些解决方法:
但最简单的解决方案是创建一个可以保存数据的viewmodel:
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());