2013-09-29 32 views
0

所以我增加了以下我_layout頁:通過public void對象傳遞ViewBag對象?

@Html.Action("ActiveMails", "Home") 

其中要求採取以下行動:

public void ActiveMails() 
{ 
    var ooo = from s in db.Message 
       where (s.SentTo.ToLower() == HttpContext.User.Identity.Name.ToLower()) 
       && s.Read != "1" 
       && s.Active == "1" 
       select s; 
    ViewBag.UnreadMessages = ooo.Count(); 
} 

的問題是,當頁面加載它不會保留ViewBag信息。有什麼辦法可以讓這個代碼在每個請求上運行,但保留ViewBag信息?

+0

變化空隙的ActionResult –

+0

看到這個:http://stackoverflow.com/questions/7737124/does-a-child-action-share-the-same-viewbag-with-its-parents-action – haim770

回答

0

爲什麼不將返回方法類型更改爲JsonResult?在這種情況下,你的ViewBag不會改變。

public ActionResult ActiveEmails() 
    { 
     var ooo = from s in db.Message 
      where (s.SentTo.ToLower() == HttpContext.User.Identity.Name.ToLower()) 
      && s.Read != "1" 
      && s.Active == "1" 
      select s; 

     return Json(ooo.Count(), JsonRequestBehavior.AllowGet); 
    }