0

I have the following in my controller (HomeController.cs) which generates a Bikes variable that I'm trying to pass into the view (Index.cshtml):

public string xmlPath = HostingEnvironment.MapPath("~/App_Data/bikes.xml");

public ActionResult Index()
{
    // get the most recent 20 records
    XDocument xml = XDocument.Load(xmlPath);

    var bikes = from b in xml.Descendants("Photo")
            select new
            {
                date = (DateTime)b.Element("Date"),
                staffno = (Int32)b.Element("StaffNo"),
                file = (string)b.Element("File"),
                crimeref = (string)b.Element("CrimeRef")
            };

    var Bikes = bikes.ToList();

    return View(Bikes);
}

I now want to loop through the results and display them on the page using Razor using the following code:

@foreach (var bikes in Bikes))
{ 
    <tr>
        <td>@(bikes.date)</td>
        <td>@(bikes.staffno)</td>
        <td>@(bikes.file)</td>
        <td>@(bikes.crimeref)</td>
    </tr>
}

However, despite passing the Bikes variable into the View() command in the controller, the Bikes list is not accessible from within my view.

Any suggestions would be very welcome, thank you :)

Nick
  • 3,745
  • 20
  • 56
  • 75
  • Put a breakpoint on "var Bikes = bikes.ToList();" and ensure your bikes is being populated correctly. – PaulBinder Aug 05 '14 at 13:53
  • Thank you Paul, I've just checked and it does seem like Bikes is being populated correctly: http://i.imgur.com/YyXFIrR.jpg – Nick Aug 05 '14 at 13:55
  • have you tried using
  • instead of ? Or do you need to have it in a table?
  • – Izekid Aug 05 '14 at 13:56
  • Probably you can find solution here http://stackoverflow.com/questions/5120317/dynamic-anonymous-type-in-razor-causes-runtimebinderexception – Vokinneberg Aug 05 '14 at 14:41