1

So I added the following to my _Layout page:

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

Which calls the following actions:

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();
}

The problem is that when the page loads it doesn't retain the ViewBag info. Is there any way I can have this code run on every request, but retain the ViewBag information?

tereško
  • 58,060
  • 25
  • 98
  • 150
kgst
  • 243
  • 1
  • 2
  • 13

1 Answers1

0

Why not change the return method type to JsonResult? In this case your ViewBag won't change.

    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);
    }
Daemon025
  • 87
  • 1
  • 11