通过公共void对象传递ViewBag对象?

时间:2013-09-29 00:37:42

标签: c# asp.net-mvc

所以我将以下内容添加到我的_Layout页面:

 @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信息吗?

1 个答案:

答案 0 :(得分:0)

为什么不将return方法类型更改为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);
    }
相关问题