0
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?

2
  • 1
    You should change either the dinner variable name or the key of the ViewData item. (Unless they contain the same thing.) Using the same name makes it hard to understand the question: imagine how much harder it will be to read your view's code. Commented May 7, 2010 at 13:42
  • @Jeff: totally, it's kind of confusing when you first read it :) @mazhar: change it to ViewData["dinners"] or sth :P Commented May 7, 2010 at 13:44

2 Answers 2

4

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 :)

Sign up to request clarification or add additional context in comments.

Comments

3
  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 
   } %>

6 Comments

this is actually completely FALSE! =/
@Artiom Chilaru - You're right. I completely mis-read the code. I'm upding my answer to reflect.
@mazhar: See, @Jeff told you it was confusing! :D
@Artiom Chilaru - Look a little better?
@Justin yup, much better ) I can't edit my previous comment though... So saying it here- it's not false anymore :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.