开发者

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜