从控制器调用Viewbag数据查看。

时间:2021-12-21 20:34:34

Controller:-

控制器:

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

            return Json(JsonRequestBehavior.AllowGet);
        }

View:-

视图:

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

This controller is called on button click.

这个控制器是按按钮按下的。

From Controller how to get value to View in viewbag.

从控制器如何获取值到viewbag。

.

1 个解决方案

#1


1  

If EmailScheduler is ajax call then you cannot use ViewBag like you have tried.

如果EmailScheduler是ajax调用,那么您不能像尝试过的那样使用ViewBag。

You need to modify your code like below.

您需要像下面这样修改代码。

Controller

控制器

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

Html

Html

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

Ajax

Ajax

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

#1


1  

If EmailScheduler is ajax call then you cannot use ViewBag like you have tried.

如果EmailScheduler是ajax调用,那么您不能像尝试过的那样使用ViewBag。

You need to modify your code like below.

您需要像下面这样修改代码。

Controller

控制器

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

Html

Html

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

Ajax

Ajax

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