Skip to main content
 首页 » 编程设计

c#之ASP.NET MVC4之以不止一种方法添加到 ViewBag

2024年02月13日13myhome

假设我有一个 Controller ,其方法返回一个 PartialView。如果我想将数据添加到 ViewBag,是否必须全部放在一个方法中?

例如:

public PartialViewResult GetPage() 
{ 
   ViewBag.Example = "Here is some text."; 
   AddMoreToViewBag(); 
   return PartialView(); 
} 
 
private void AddMoreToViewBag() 
{ 
   ViewBag.Example2 = "More text"; 
} 

在上面的示例中,Example1 和 Example2 是否都可用于返回的 PartialView?

请您参考如下方法:

In the above example, will both Example1 and Example2 be available to the PartialView that is returned?

是的,它是可用的。