开发者

asp.net viewdata

开发者 https://www.devze.com 2022-12-29 06:28 出处:网络
public ActionResult AddDinner() { Dinner dinner = dinnerRepository.GetDinner(id); ViewData[\"dinner\"] = repository.AllDinners();
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

精彩评论

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