asp.net viewdata
public ActionResult AddDinner()
{
Dinner dinner = dinnerRepository.GetDinner(id);
ViewData["dinner"] = repository.AllDinners();
return View(di开发者_开发知识库nner);
}
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?
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 :)
- Yes. Both
dinner
and ViewData["dinner"] will be available on the page. You can accessdinner
via theModel
in theView
.
2.
<% foreach(Dinner d in (IEnumerable<Dinner>)ViewData["dinner"])
{
// Code goes here
} %>
精彩评论