views:

462

answers:

3

I am trying to get NerdDinner to work, and having a bit of trouble.

I have quick watched at the point where the actionResult for index happens, and seen that the items from the dinnerRepository are on the right side of the asignment operator.

However it seems when I step through to the next line of the code, dinners, which is passed to the view, does not contain the recordset.

Could anyone point me out where I am going wrong.

The result is that the page renders perfectly, but no records are shown.

namespace NerdDinner.Controllers
{
    public class DinnersController : Controller
    {       
        DinnerRepository dinnerRepository = new DinnerRepository();  

        // GET: /Dinners/
        public ActionResult Index()
        {
           var dinners = dinnerRepository.FindUpcomingDinners().ToList();

           return View("Index", dinners);            
        }

        // GET: /Dinners/Details/2
        public ActionResult Details(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);

            if (dinner == null)
                return View("NotFound");
            else
                return View("Details", dinner);
        }
    }
}

And here is the view code as well

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"      Inherits="System.Web.Mvc.ViewPage<IEnumerable<NerdDinner.Models.Dinner>>" %>



<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">

<h2>Upcoming Dinners</h2>

<ul>
<% foreach (var dinner in Model)
   {%>

<li> 
<%= Html.Encode(dinner.Title)%>
on
<%= Html.Encode(dinner.EventDate.ToShortDateString())%>
@
<%= Html.Encode(dinner.EventDate.ToShortTimeString())%>


</li>
<% } %>

</ul>
</asp:Content>

And the model code as well

public class DinnerRepository { private NerdDinnerDataContext db = new NerdDinnerDataContext();

    // Query Methods

    public IQueryable<Dinner> FindAllDinners()
    {
        return db.Dinners;
    }

    public IQueryable<Dinner> FindUpcomingDinners()
    {
        return from dinner in db.Dinners
               where dinner.EventDate > DateTime.Now
               orderby dinner.EventDate
               select dinner;
    }

    public Dinner GetDinner(int id)
    {
        return db.Dinners.SingleOrDefault(d => d.DinnerID == id);
    }

    // Insert/Delete Methods
A: 

I am wondering if the ViewData of the view is not aligned right with the dinners object that is being passed in? Try switching the dinners being passed in via the View() call to ViewData["dinners"] = dinners. Then update the UI code to pull the dinners out the view data instead of the Model where it is probably currently referencing.

Can you post your view code as well?

Andrew Siemer
A: 

Check your DinnersRepository too. If this line:

var dinners = dinnerRepository.FindUpcomingDinners().ToList();

is returning en 'empty recordset', you should investigate why, by stepping into (shortcut F11) the repository method.

Add a quick watch on 'dinners' and step over that line, you should definitely see a non-empty list if there was any data to be had.

Dan
When I quick watch on dinnerRepository I see the record set, but I dont see it on dinners, but I could assume this is becuase the statement has not run yet.
optician
If you step to that line, there should be a results view option, when you expand that option do you see any records?
Trevor Abell
+1  A: 

I just recently went through that tutorial and got nearly everything working. I would walk through these steps.

  1. (obvious) Check that there is data in the database to be grabbed.
  2. Check that your DinnerRepository is returning an IQueryable object of all of the dinners, debug mode should allow you to expand the query. It sounds like you did this but you were a little vague, you were saying the right side argument contains the Dinner items but the variable you assign it to does not contain the items on the next line?
  3. Make sure your viewpage inherits from a strongly typed view. E.g. System.Web.Mvc.ViewPage<IEnumerable<NerdDinner.Models.Dinner>>
Trevor Abell
The problem was that the dates in the db were in the past, but it didn't make sense to me how it would look like they were there when I quick watched it.I worked it out when I hooked it up to the reference db source, and it worked, but only showed about half the records! Feel very silly now.But happy to continue with the tutorial
optician