从控制器调用Viewbag数据进行查看

时间:2017-05-29 06:01:37

标签: jquery ajax asp.net-mvc viewbag

  

控制器: -

[HttpPost]
        public ActionResult EmailScheduler()
        {
            long lCustDesignID = 1;
                int countProduct = gateWay.TotalCountOfCustomers(lCustDesignID);
                ViewBag.ItemCount = countProduct;

            return Json(JsonRequestBehavior.AllowGet);
        }
  

查看: -

<h4>Number Of Records - <span>@ViewBag.ItemCount</span> </h4>
  

点击按钮调用此控制器。

     

从Controller如何获取值到Viewbag中的View。

1 个答案:

答案 0 :(得分:1)

如果EmailScheduler是ajax电话,那么您就不能像尝试过那样使用ViewBag

您需要修改以下代码。

<强>控制器

[HttpPost]
public ActionResult EmailScheduler()
{
   long lCustDesignID = 1;
   int countProduct = gateWay.TotalCountOfCustomers(lCustDesignID);                    
   return Json(countProduct,JsonRequestBehavior.AllowGet);
}

<强> HTML

<h4>Number Of Records - <span id="spnCount"></span> </h4>

<强>的Ajax

$.ajax({
   //....
   success: function(data){
      $('#spnCount').text(data);
   }
})
相关问题