【问题标题】:Passing ViewBag objects through public void objects?通过公共 void 对象传递 ViewBag 对象?
【发布时间】:2013-09-29 00:37:42
【问题描述】:

所以我在我的 _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 信息?

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

为什么不将返回方法类型改为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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2020-02-13
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多