tags:

views:

37

answers:

2
public ActionResult AddDinner()
{
  Dinner dinner = dinnerRepository.GetDinner(id);
  ViewData["dinner"] = repository.AllDinners();

  return View(dinner);
}

1) First, are both the dinner object and the ViewData["dinner"] is passing to the view?

2) Second, how would I iterate over the ViewData["dinner"] in the view?

+3  A: 
  1. Yes. Both dinner and ViewData["dinner"] will be available on the page. You can access dinner via the Model in the View.

2.

<% foreach(Dinner d in (IEnumerable<Dinner>)ViewData["dinner"])
   {
       // Code goes here 
   } %>
Justin Niessner
this is actually completely FALSE! =/
Artiom Chilaru
@Artiom Chilaru - You're right. I completely mis-read the code. I'm upding my answer to reflect.
Justin Niessner
@mazhar: See, @Jeff told you it was confusing! :D
Artiom Chilaru
@Artiom Chilaru - Look a little better?
Justin Niessner
@Justin yup, much better )I can't edit my previous comment though... So saying it here- it's not false anymore :P
Artiom Chilaru
I am not able to get any intellisence of the object by using d? How would I do that
mazhar kaunain baig
+4  A: 

1) Yes, both will be available in your view. So nothing to worry :)

2) While the data you pass to the View() method will be available from the view's Model object, you can access all the data you set in the ViewData[] set by reading them from... ViewData[]! ^_^

So anywhere in your view, you can do this:

<% foreach(Dinner d in ViewData["dinner"] as IEnumerable<Dinner>)
{
    RenderPartial("Dinner", d);
} %>

Or something like that :)

Artiom Chilaru