2013-09-29 33 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); 
    }