I have a JSON function to return the following data and it is what I expected:
[{"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}}]
but when I try to display the Title on the view page I am having problems. This is the view page code:
<table> @foreach (var showList in Model) {<tr><td>@showList.Title</td></tr>}<.table>
This is the ActionResult I have been using but when I got through to the view I could not get the data to display. I used the JSON function as above to double check that I had the right data and was not trying to call something that was not there.
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);
}
The error I get is:
RuntimeBinderException was unhandled by code
‘object’ does not contain a definition for Title
With the expression
new { ... }you are creating an anonymous type which is basically not supported to pass in theViewmethod as a Model.There are a few workarounds:
But the easiest solution is to create a viewmodel which will hold your data:
And return this from your query: